linux怎么查看本机内存大小
242
2023-01-21
ssm整合shiro使用详解
这里有详细的ssm整合shiro步骤,需要先搭建ssm框架,教程在
https://jb51.net/article/195130.htm
整合shiro:
1.在pom.xml中引入依赖
2.新建并配置缓存ehcache.xml
eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="0" overflowToDisk="false" statistics="true">
eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="0" overflowToDisk="false" statistics="true">
eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="0" overflowToDisk="false" statistics="true">
maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" /> maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> 3.在spring配置文件applicationContext.xml配置shiro xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx.xsd"> /noPermission.jsp=anon /login.jsp=anon /login = anon /logout = logout /**=authc /**=user,roles[admin] depends-on= "lifecycleBeanPostProcessor"/> 4.自定义realm(内部定义认证和授权的逻辑代码) package com.liuzhan.relams; import com.liuzhan.entity.Users; import com.liuzhan.service.UserService; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; import java.util.HashSet; import java.util.List; import java.util.Set; public class ShiroRealm extends AuthorizingRealm { @Autowired UserService userService; /** * 用于授权。 * @param principalCollection * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { System.out.println("开始授权"); String uName = principalCollection.getPrimaryPrincipal().toString() ; SimpleAuthorizationInfo info = new SimpleAuthorizationInfo() ; List //查询当前用户的角色,放在roleName中 Set roleName.add(list.get(0).getRole()); //查询角色具有的权限,放在permissions中 Set 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 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中相关代码 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; @Controller public 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"; } } 这里到dao service entity就不再粘贴代码了 6.数据库连接配置相关 jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm-shiro?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.user=root jdbc.password=root applicationContext.xml 项目中用到的是通用mapper,实体类的类名和数据库的表名对应,实体类的属性和数据库表的字段名对应 测试 运行项目 因为我shiro过滤链配置的是只有角色为admin的能进首页,角色为user不能进首页,会被拦截(数据库jake为admin,tom为user) 运行结果:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~