iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >怎么在shiro使用ssm框架
  • 353
分享到

怎么在shiro使用ssm框架

2023-06-14 18:06:14 353人浏览 安东尼
摘要

本篇文章为大家展示了怎么在shiro使用SSM框架,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1.在pom.xml中引入依赖<!--  shiro -->

本篇文章为大家展示了怎么在shiro使用SSM框架,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1.在pom.xml中引入依赖

<!--  shiro -->    <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->    <dependency>      <groupId>org.apache.shiro</groupId>      <artifactId>shiro-core</artifactId>      <version>1.6.0</version>    </dependency>    <!-- Https://mvnrepository.com/artifact/org.apache.shiro/shiro-WEB -->    <dependency>      <groupId>org.apache.shiro</groupId>      <artifactId>shiro-web</artifactId>      <version>1.6.0</version>    </dependency>    <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->    <dependency>      <groupId>org.apache.shiro</groupId>      <artifactId>shiro-spring</artifactId>      <version>1.6.0</version>    </dependency>    <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-ehcache -->    <dependency>      <groupId>org.apache.shiro</groupId>      <artifactId>shiro-ehcache</artifactId>      <version>1.6.0</version>    </dependency>

2.新建并配置缓存ehcache.xml

怎么在shiro使用ssm框架

<ehcache>    <!-- Sets the path to the directory where cache .data files are created.         If the path is a Java System Property it is replaced by         its value in the running VM.         The following properties are translated:         user.home - User's home directory         user.dir - User's current working directory         java.io.tmpdir - Default temp file path -->    <diskStore path="java.io.tmpdir"/>        <cache name="authorizationCache"           eternal="false"           timeToIdleSeconds="3600"           timeToLiveSeconds="0"           overflowToDisk="false"           statistics="true">    </cache>    <cache name="authenticationCache"           eternal="false"           timeToIdleSeconds="3600"           timeToLiveSeconds="0"           overflowToDisk="false"           statistics="true">    </cache>    <cache name="shiro-activeSessionCache"           eternal="false"           timeToIdleSeconds="3600"           timeToLiveSeconds="0"           overflowToDisk="false"           statistics="true">    </cache>    <!--Default Cache configuration. These will applied to caches programmatically created through        the CacheManager.        The following attributes are required for defaultCache:        maxInMemory       - Sets the maximum number of objects that will be created in memory        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element                            is never expired.        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used                            if the element is not eternal. Idle time is now - last accessed time        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used                            if the element is not eternal. TTL is now - creation time        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache                            has reached the maxInMemory limit.        -->    <defaultCache        maxElementsInMemory="10000"        eternal="false"        timeToIdleSeconds="120"        timeToLiveSeconds="120"        overflowToDisk="true"        />    <!--Predefined caches.  Add your cache configuration settings here.        If you do not have a configuration for your cache a WARNING will be issued when the        CacheManager starts        The following attributes are required for defaultCache:        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.        maxInMemory       - Sets the maximum number of objects that will be created in memory        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element                            is never expired.        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used                            if the element is not eternal. Idle time is now - last accessed time        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used                            if the element is not eternal. TTL is now - creation time        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache                            has reached the maxInMemory limit.        -->    <!-- Sample cache named sampleCache1        This cache contains a maximum in memory of 10000 elements, and will expire        an element if it is idle for more than 5 minutes and lives for more than        10 minutes.        If there are more than 10000 elements it will overflow to the        disk cache, which in this configuration will Go to wherever java.io.tmp is        defined on your system. On a standard linux system this will be /tmp"        -->    <cache name="sampleCache1"        maxElementsInMemory="10000"        eternal="false"        timeToIdleSeconds="300"        timeToLiveSeconds="600"        overflowToDisk="true"        />    <!-- Sample cache named sampleCache2        This cache contains 1000 elements. Elements will always be held in memory.        They are not expired. -->    <cache name="sampleCache2"        maxElementsInMemory="1000"        eternal="true"        timeToIdleSeconds="0"        timeToLiveSeconds="0"        overflowToDisk="false"        />    <!-- Place configuration for your caches following --></ehcache>

3.在spring配置文件applicationContext.xml配置shiro

怎么在shiro使用ssm框架

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">    <!-- spring 配置文件  主要配置和业务逻辑有关的 -->    <context:property-placeholder location="classpath:dbconfig.properties"/>    <!-- 数据源 -->    <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>        <property name="driverClass" value="${jdbc.driverClass}"></property>        <property name="user" value="${jdbc.user}"></property>        <property name="passWord" value="${jdbc.password}"></property>    </bean>    <context:component-scan base-package="com.liuzhan">        <!-- 不能扫描控制器 -->    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>    <!-- 配置和mybatis的整合 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 指定mybatis 全局配置文件的位置 -->        <property name="configLocation" value="classpath:mybatis-config.xml"></property>        <property name="dataSource" ref="pooledDataSource"></property>        <!-- 指定mybatismapper文件的位置 --><!--        <property name="mapperLocations" value="classpath:mapper    @Override    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {        System.out.println("开始授权");        String uName = principalCollection.getPrimaryPrincipal().toString() ;        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo() ;        List<Users> list=userService.loginCheck(uName);        //查询当前用户的角色,放在roleName中        Set<String> roleName = new HashSet<>();        roleName.add(list.get(0).getRole());        //查询角色具有的权限,放在permissions中        Set<String> permissions = new HashSet<>();        permissions.add("manage other users");        //把角色和权限放在授权类的对象中        info.addRole(list.get(0).getRole());        info.addStringPermission("manage other users");        System.out.println("当前用户角色:"+info.getRoles());        return info;    }    //认证    //用户输入用户名和密码后,在controller调用login(token),进行认证,这边通过用户名查询密码    //和token中用户名和密码进行比对,成功认证或失败    @Override    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {        System.out.println("开始登录认证");        //获取用户名,去数据库取对应密码        String uName = (String) authenticationToken.getPrincipal();        List<Users> list=userService.loginCheck(uName);        if(list.size()>0){            System.out.println("用户存在");            String uPwd=list.get(0).getuPwd();            // 用户名存在,去数据库中去获取密码            // 然后和token的用户名和密码对比            // 第三个参数是选择realm,当有多个自定义realm时有用            SimpleAuthenticationInfo info=new                    SimpleAuthenticationInfo(uName, uPwd, "ShiroRealm");            return info;        }        else{            System.out.println("用户不存在");            return null;        }    }}

5.controller中相关代码

怎么在shiro使用ssm框架

package com.liuzhan.controller;import com.liuzhan.entity.Users;import com.liuzhan.service.UserService;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.subject.Subject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class UserController {    @Autowired    UserService userService;    @RequestMapping("/login")    public String loginCheck(Users users){        Subject subject=SecurityUtils.getSubject();        if(!subject.isAuthenticated()) {            UsernamePasswordToken token=new UsernamePasswordToken(users.getuName(),users.getuPwd());            token.setRememberMe(true);            try {                //执行登录,会调用认证方法,如果认证失败,会抛出异常,执行catch                subject.login(token);            } catch (AuthenticationException e) {                // TODO Auto-generated catch block                System.out.println("登录失败:"+e.getMessage());                return "login";            }        }        //login(token)认证成功会执行这些语句        System.out.println("登录成功");        //这里如果直接 return "WEB-INF/jsp/index"的话,由于默认转发,地址栏不变,还是http://localhost:8080/ssm_shiro_war_exploded/login        //仍然执行/login = anon过滤规则,不会执行下面的权限验证过滤规则 /**=user,roles[admin]        //因此需要使用重定向"redirect:index",才能让 /**=user,roles[admin]过滤规则生效        return "redirect:index";    }    @RequestMapping("/index")    public String index() {       return "WEB-INF/jsp/index";    }}

怎么在shiro使用ssm框架

怎么在shiro使用ssm框架

这里到dao service entity就不再粘贴代码了

6.数据库连接配置相关

怎么在shiro使用ssm框架

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm-shiro?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8jdbc.driverClass=com.Mysql.cj.jdbc.Driverjdbc.user=rootjdbc.password=root

applicationContext.xml

怎么在shiro使用ssm框架

项目中用到的是通用mapper,实体类的类名和数据库的表名对应,实体类的属性和数据库表的字段名对应

怎么在shiro使用ssm框架

测试

运行项目

怎么在shiro使用ssm框架

因为我shiro过滤链配置的是只有角色为admin的能进首页,角色为user不能进首页,会被拦截(数据库jake为admin,tom为user)

怎么在shiro使用ssm框架

运行结果:

怎么在shiro使用ssm框架

怎么在shiro使用ssm框架

上述内容就是怎么在shiro使用ssm框架,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网精选频道。

--结束END--

本文标题: 怎么在shiro使用ssm框架

本文链接: https://www.lsjlt.com/news/274190.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么在shiro使用ssm框架
    本篇文章为大家展示了怎么在shiro使用ssm框架,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1.在pom.xml中引入依赖<!--  shiro -->...
    99+
    2023-06-14
  • Java SSM框架怎么应用
    这篇文章主要介绍了Java SSM框架怎么应用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Java SSM框架怎么应用文章都会有所收获,下面我们一起来看看吧。一、什么是SSM框架?SSM框...
    99+
    2023-07-05
  • ssm框架如何使用redis
    ssm框架使用redis的示例:导入Redis相关jar包,代码:<!-- redis相关 -->    <dependency>  &...
    99+
    2024-04-02
  • ssm整合shiro使用详解
    目录整合shiro:1.在pom.xml中引入依赖2.新建并配置缓存ehcache.xml3.在spring配置文件applicationContext.xml配置shiro4.自定...
    99+
    2024-04-02
  • Java shiro安全框架使用介绍
    目录1.shiro安全框架1.1 什么是权限管理1.2 什么是身份认证1.3 什么是授权1.4 认证授权框架有哪些2.使用shiro完成认证工作2.1 shiro中认证的关键对象2....
    99+
    2024-04-02
  • 安全框架Shiro怎么配置
    这篇文章主要介绍“安全框架Shiro怎么配置”,在日常操作中,相信很多人在安全框架Shiro怎么配置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”安全框架Shiro怎么配置”的疑惑有所帮助!接下来,请跟着小编...
    99+
    2023-06-26
  • java SSM框架 代码生成器 websocket即时通讯 shiro redis 后台框架源码
    A代码编辑器,在线模版编辑,仿开发工具编辑器,pdf在线预览,文件转换编码B 集成代码生成器 [正反双向](单表、主表、明细表、树形表,快速开发利器)+快速表单构建器freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面、...
    99+
    2023-06-02
  • Java中Apache Shiro安全框架怎么用
    这篇文章将为大家详细讲解有关Java中Apache Shiro安全框架怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一、Shiro简介:Apache Shiro是一个Java的安全(权限)框架。Sh...
    99+
    2023-06-25
  • 怎么用Springboot快速整合shiro安全框架
    这篇文章主要介绍“怎么用Springboot快速整合shiro安全框架”,在日常操作中,相信很多人在怎么用Springboot快速整合shiro安全框架问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Sp...
    99+
    2023-07-05
  • 如何在java项目中应用SSM框架
    本篇文章给大家分享的是有关如何在java项目中应用SSM框架,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。配置环境变量配置完后,使用命令行输入mvn -version查看是否配...
    99+
    2023-05-30
    java ssh框架
  • Java安全框架——Shiro的使用详解(附springboot整合Shiro的demo)
    目录Shiro简介 Shiro快速入门 SpringBoot-Shiro整合(最后会附上完整代码) 附上最后的完整代码 Shiro整合mybatis 认证搞完了,我们再来看看授权 S...
    99+
    2024-04-02
  • 怎么快速搭建一个SSM框架
    怎么快速搭建一个SSM框架?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一、我用的是idea二、首先创建一个maven项目,结构如下:三、开始写配置文件pom文...
    99+
    2023-05-31
    ssm
  • ssm框架数据库连接怎么配置
    在SSM(Spring+SpringMVC+MyBatis)框架中,配置数据库连接主要是在MyBatis的配置文件中进行。以下是配置...
    99+
    2024-04-02
  • 怎么在php中使用Zend框架
    这篇文章给大家介绍怎么在php中使用Zend框架,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。PHP开发环境搭建工具有哪些一、phpStudy,是一个新手入门最常用的开发环境。二、WampServer,WampServ...
    99+
    2023-06-14
  • Retrofit框架怎么在Android中使用
    Retrofit框架怎么在Android中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。Android是什么Android是一种基于Linux内核的自由及开放源代码的操作系...
    99+
    2023-06-14
  • 怎么在java中使用mybatis框架
    怎么在java中使用mybatis框架?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。Java的特点有哪些Java的特点有哪些1.Java语言作为静态面向对象编程...
    99+
    2023-06-14
  • 怎么在java中使用Spring框架
    怎么在java中使用Spring框架?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。Java是什么Java是一门面向对象编程语言,可以编写桌面应用程序、Web应用程序、分布式系统...
    99+
    2023-06-14
  • 怎么在php中使用swoft框架
    本篇文章为大家展示了怎么在php中使用swoft框架,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。PHP开发环境搭建工具有哪些一、phpStudy,是一个新手入门最常用的开发环境。二、WampSer...
    99+
    2023-06-14
  • shiro框架04会话管理+缓存管理+Ehcache使用
    目录 一、会话管理 1.基础组件 1.1 SessionManager 1.2 SessionListener 1.3 SessionDao 1.4 会话验证 1.5 案例 二、缓存管理 1、为什么要使用缓存 2、什么是ehcache 3、...
    99+
    2023-10-10
    缓存 java 开发语言
  • 如何使用eclipse+maven一步步搭建SSM框架
    这篇文章将为大家详细讲解有关如何使用eclipse+maven一步步搭建SSM框架,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。SSM (SSM 框架集)SSM(Spring+SpringMVC+MyBa...
    99+
    2023-05-30
    eclipse maven ssm
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作