Spring Security3源码分析(9)-SecurityContextHolderAwareRequestFilter分析

SecurityContextHolderAwareRequestFilter过滤器对应的类路径为 
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter 

从类名称可以猜出这个过滤器主要是包装请求对象request的,看源码 
Java代码   收藏代码
  1. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)  
  2.         throws IOException, ServletException {  
  3.     chain.doFilter(new SecurityContextHolderAwareRequestWrapper((HttpServletRequest) req, rolePrefix), res);  
  4. }  


SecurityContextHolderAwareRequestWrapper类对request包装的目的主要是实现servlet api的一些接口方法isUserInRole、getRemoteUser 
Java代码   收藏代码
  1. //从SecurityContext中获取认证实体Authentication  
  2. private Authentication getAuthentication() {  
  3.     Authentication auth = SecurityContextHolder.getContext().getAuthentication();  
  4.   
  5.     if (!authenticationTrustResolver.isAnonymous(auth)) {  
  6.         return auth;  
  7.     }  
  8.   
  9.     return null;  
  10. }  
  11.   
  12. //实现getRemoteUser方法。首先获取认证实体,再从认证实体中获取登录账号  
  13. @Override  
  14. public String getRemoteUser() {  
  15.     Authentication auth = getAuthentication();  
  16.   
  17.     if ((auth == null) || (auth.getPrincipal() == null)) {  
  18.         return null;  
  19.     }  
  20.   
  21.     if (auth.getPrincipal() instanceof UserDetails) {  
  22.         return ((UserDetails) auth.getPrincipal()).getUsername();  
  23.     }  
  24.   
  25.     return auth.getPrincipal().toString();  
  26. }  
  27.   
  28. //实现getUserPrincipal方法  
  29. @Override  
  30. public Principal getUserPrincipal() {  
  31.     Authentication auth = getAuthentication();  
  32.   
  33.     if ((auth == null) || (auth.getPrincipal() == null)) {  
  34.         return null;  
  35.     }  
  36.   
  37.     return auth;  
  38. }  
  39.   
  40. //判断是否授权。这里注意一下rolePrefix,就是角色的前缀  
  41. private boolean isGranted(String role) {  
  42.     Authentication auth = getAuthentication();  
  43.   
  44.     if( rolePrefix != null ) {  
  45.         role = rolePrefix + role;  
  46.     }  
  47.   
  48.     if ((auth == null) || (auth.getPrincipal() == null)) {  
  49.         return false;  
  50.     }  
  51.   
  52.     Collection<GrantedAuthority> authorities = auth.getAuthorities();  
  53.   
  54.     if (authorities == null) {  
  55.         return false;  
  56.     }  
  57.   
  58.   
  59.     for (GrantedAuthority grantedAuthority : authorities) {  
  60.         if (role.equals(grantedAuthority.getAuthority())) {  
  61.             return true;  
  62.         }  
  63.     }  
  64.   
  65.     return false;  
  66. }  
  67.   
  68. //实现isUserInRole  
  69. @Override  
  70. public boolean isUserInRole(String role) {  
  71.     return isGranted(role);  
  72. }  


这个过滤器看起来很简单。目的仅仅是实现java ee中servlet api一些接口方法。 
一些应用中直接使用getRemoteUser方法、isUserInRole方法,在使用spring security时其实就是通过这个过滤器来实现的。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值