博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第八讲 shiro 整合 ssm
阅读量:4319 次
发布时间:2019-06-06

本文共 10976 字,大约阅读时间需要 36 分钟。

1、整合ssm并且实现用户登录和菜单权限

2、将shiro整合到ssm中

  (1)添加shiro相关jar包

  (2)在web.xml中添加shiro配置

1     
2
3
4
shiroFilter
5
org.springframework.web.filter.DelegatingFilterProxy
6
7
8
targetFilterLifecycle
9
true
10
11 12
13
14
targetBeanName
15
shiroFilter
16
17 18
19 20
21
shiroFilter
22
/*
23
24 25

  (3)添加applicationContext-shiro.xml

/toLogin=anon /login=authc /logout=logout

           /js/**=anon

           /css/**=anon
           /images/**=anon

/**=authc                                

  (4)修改LoginController中的登录方法

package com.sun123.template.conrtroller;import javax.servlet.http.HttpServletRequest;import org.apache.shiro.authc.IncorrectCredentialsException;import org.apache.shiro.authc.UnknownAccountException;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class LoginController {    @RequestMapping(value = {"/","index"})    public ModelAndView index() {        return new ModelAndView("index");    }        //去登录页面    @RequestMapping("/toLogin")    public ModelAndView toLogin() {        return new ModelAndView("login");    }        //登录    @RequestMapping("/login")    public ModelAndView login(HttpServletRequest request) {        System.out.println("========login=======");        ModelAndView mv = new ModelAndView("login");        String className = (String)request.getAttribute("shiroLoginFailure");        if (UnknownAccountException.class.getName().equals(className)) {            //抛出自定义异常            mv.addObject("msg","用户名或密码错误");        }else if (IncorrectCredentialsException.class.getName().equals(className)) {            //抛出自定义异常            mv.addObject("msg","用户名或密码错误");        } else {            //抛出自定义异常            mv.addObject("msg","系统异常");        }                return mv;            }        //访问被拒绝    @RequestMapping("/refuse")    public ModelAndView refuse() {        return new ModelAndView("refuse");    }}

  (5)添加自定义Realm:UserRealm.java

package com.sun123.template.realm;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.realm.AuthorizingRealm;import org.apache.shiro.subject.PrincipalCollection;public class UserRealm extends AuthorizingRealm {    @Override    public String getName() {        // TODO Auto-generated method stub        return "userRealm";    }        //获取认证信息    @Override    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {        System.out.println("---------认证----------------");        String username = token.getPrincipal().toString();        String pwd = "1111";                return new SimpleAuthenticationInfo(username,pwd,getName());    }        //获取授权信息    @Override    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {                        return null;    }    }

 

3、修改UserRealm实现自定义认证

1 package com.sun123.template.realm; 2  3 import org.apache.shiro.authc.AuthenticationException; 4 import org.apache.shiro.authc.AuthenticationInfo; 5 import org.apache.shiro.authc.AuthenticationToken; 6 import org.apache.shiro.authc.SimpleAuthenticationInfo; 7 import org.apache.shiro.authz.AuthorizationInfo; 8 import org.apache.shiro.realm.AuthorizingRealm; 9 import org.apache.shiro.subject.PrincipalCollection;10 import org.springframework.beans.factory.annotation.Autowired;11 12 import com.sun123.template.entity.User;13 import com.sun123.template.service.UserService;14 15 public class UserRealm extends AuthorizingRealm {16 17     @Autowired18     private UserService userService;19     20     @Override21     public String getName() {22         // TODO Auto-generated method stub23         return "userRealm";24     }25     26     //获取认证信息27     @Override28     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {29         System.out.println("---------认证----------------");30         String username = token.getPrincipal().toString();31         User user = userService.findByUserName(username); 32         return new SimpleAuthenticationInfo(user,user.getPassword(),getName());33     }34     35     //获取授权信息36     @Override37     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {38 39         return null;40     }41 42     43 44 }

4、凭证匹配器配置

1     
2
3
4
5
6
8
9
10
11

  UserRealm.java要相应改变

1 package com.sun123.template.realm; 2  3 import org.apache.shiro.authc.AuthenticationException; 4 import org.apache.shiro.authc.AuthenticationInfo; 5 import org.apache.shiro.authc.AuthenticationToken; 6 import org.apache.shiro.authc.SimpleAuthenticationInfo; 7 import org.apache.shiro.authz.AuthorizationInfo; 8 import org.apache.shiro.realm.AuthorizingRealm; 9 import org.apache.shiro.subject.PrincipalCollection;10 import org.apache.shiro.util.ByteSource;11 import org.springframework.beans.factory.annotation.Autowired;12 13 import com.sun123.template.entity.User;14 import com.sun123.template.service.UserService;15 16 public class UserRealm extends AuthorizingRealm {17 18     @Autowired19     private UserService userService;20     21     @Override22     public String getName() {23         // TODO Auto-generated method stub24         return "userRealm";25     }26     27     //获取认证信息28     @Override29     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {30         System.out.println("---------认证----------------");31         String username = token.getPrincipal().toString();32         User user = userService.findByUserName(username); 33         return new SimpleAuthenticationInfo(user,user.getPassword(),ByteSource.Util.bytes(user.getPasswordSalt()),getName());34     }35     36     //获取授权信息37     @Override38     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {39 40         return null;41     }42 43     44 45 }

5、logout配置,默认退出后跳转到根路径下,如果需要改变则需重新配置logout过滤器,过滤器的id不能改变,只能为logout

1     
3
4
5
6
7
8
9
10
11
12
15
16
17
18 /toLogin=anon19 /login=authc20 /logout=logout21 /js/**=anon22 /css/**=anon23 /images/**=anon24 /**=authc25
26
27
28 29
30
31
32
33
34 35
36
37
38

6、改变登录时的表单域名称,需要重新配置authc过滤器

1     
3
4
5
6
7
8
9
10
11
12
15
16
17
18 /toLogin=anon19 /login=authc20 /logout=logout21 /js/**=anon22 /css/**=anon23 /images/**=anon24 /**=authc25
26
27
28 29
30
31
32
33

  登录页面的改变:

1 
2
4 username:
5 password:
6
7

 7、授权

  (1)修改自定义Realm进行权限检查

  

  (2)在spring-mvc的配置文件中,添加AOP代理,并且添加异常处理

  

1     
2
3
4
6
7

  在controller的处理方法中,添加权限检测的注解

  

  在jsp页面中,添加按钮权限检测,首先需要导入shiro的标签库

  

  在需要检查权限的地方,使用shiro标签

  

 8、缓存

  每次检查权限都会到数据库中获取权限,这样效率很低。可以通过设置缓存来解决该问题。Shiro可以和ehcache或者redis集成。这里使用ehcache来缓存数据。

  (1)将ehcache,jar导入系统。

  (2)shiro默认集成了一个ehcache的配置文件。也可以自己添加一个进行配置,放入resources下。

  ehcache.xml:

1 
2
3
12

  (3)在applicationContext-shiro.xml中,添加cacheManager的配置

1     
2
4
5
6
7
8
9
10

  (4)如果在运行过程中,主体的权限发生了改变,那么应该从spring容器中调用realm中的清理缓存方法,进行清理。

  UserRealm.java中添加清理缓存方法

1     //清理缓存方法2     protected void clearCache() {3         Subject subject = SecurityUtils.getSubject();4         super.clearCache(subject.getPrincipals());5     }

9、会话管理

1     
2
4
5
6
7
8
9
10
11
12 13
14
15
16
17
18
19

10、记住我

  (1)将用户类实现序列化接口,该类的引用类也必须实现序列化接口

  (2)设置登录时表单中“记住我”的域名

1     
2
3
4
5
6

  (3)设置“记住我”管理器

1     
2
4
5
6
7
8
9
10
11
12
13 14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

  (4)在过滤器链中配置哪些资源通过记住我就可以再次访问

1         
4
5
6
7 /toLogin=anon 8 /login=authc 9 /logout=logout10 /js/**=anon11 /css/**=anon12 /images/**=anon13 /easyui/**=anon14 /index=user15 /**=authc16
17

  (5)jsp页面设置

1 
2
4 username:
5 password:
6
记住我
7
8

 

转载于:https://www.cnblogs.com/116970u/p/10956674.html

你可能感兴趣的文章
oracle的级联更新、删除
查看>>
多浏览器开发需要注意的问题之一
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
springMVC中一个class中的多个方法
查看>>
Linux系统安装出错后出现grub rescue的修复方法
查看>>
线段树模板整理
查看>>
[教程][6月4日更新]VMware 8.02虚拟机安装MAC lion 10.7.3教程 附送原版提取镜像InstallESD.iso!...
查看>>
[iOS问题归总]iPhone上传项目遇到的问题
查看>>
Python天天美味(总) --转
查看>>
Spring Framework tutorial
查看>>
【VS开发】win7下让程序默认以管理员身份运行
查看>>
【机器学习】Learning to Rank 简介
查看>>
Unity 使用实体类
查看>>
【转】通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件...
查看>>
MySQL常见注意事项及优化
查看>>