页面获取Spring Security登录用户

1.在session中取得spring security的登录用户名如下:

[html] view plain copy
  1. ${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}  

       spring security 把SPRING_SECURITY_CONTEXT 放入了session 没有直接把username 放进去。下面一段代码主要描述的是session中的存的变量,

存跳转时候的URLsession {SPRING_SECURITY_SAVED_REQUEST_KEY=SavedRequest[http://localhost:8080/AVerPortal/resourceAction/resourceIndex.action]}
存的是登录成功时候session中存的信息:

        session {SPRING_SECURITY_CONTEXT=org.springframework.security.context.SecurityContextImpl@87b16984: Authentication: org.springframework.security.providers.cas.CasAuthenticationToken@87b16984: Principal: com.avi.casExtends.UserInfo@ff631d80: Username: test; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_ADMIN; Password: [PROTECTED]; Authenticated: true; Details: org.springframework.security.ui.WebAuthenticationDetails@12afc: RemoteIpAddress: 127.0.0.1; SessionId: AE56E8925195DFF4C50ABD384574CCEA; Granted Authorities: ROLE_ADMIN Assertion: org.jasig.cas.client.validation.AssertionImpl@661a11 Credentials (Service/Proxy Ticket): ST-3-1lX3acgZ6HNgmhvjXuxB-cas, userId=2, userName=test}

2.在页面端用tag获取:

[html] view plain copy
  1. <%@ taglib prefix='security' uri='http://www.springframework.org/security/tags'%>  
  2.   
  3. <security:authentication property="principal.username"></security:authentication>   

或者

[html] view plain copy
  1. <security:authorize ifAllGranted="ROLE_ADMIN">  
  2.   
  3.  <security:authentication property="principal.username"></security:authentication>   
  4.   
  5. </security:authorize>  

或者取session中的值:

[html] view plain copy
  1. ${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}  

3.在后台获取

[html] view plain copy
  1. UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()  
  2.     .getAuthentication()  
  3.     .getPrincipal();  
  4.   
  5. userDetails.getUsername()  


 

      如果想要获取更多的信息:得扩展userDetails的默认实现类user类和UserDetailsService接口

     由于springsecurity是把整个user信息放入session中的即:session.SPRING_SECURITY_CONTEXT.authentication.principal。这个就是代表着user对象。



http://blog.csdn.net/zmx729618/article/details/51914836


二、标签使用

Security共有三类标签authorize  authentication   accesscontrollist  ,第三个标签不在这里研究

前提:项目需要引用spring-security-taglibs-3.05,jstl1.2的jar包,页面加入:<%@ taglib prefix=”sec” uri=”http://www.springframework.org/security/tags” %>

本文配置

一、authorize

对应的类: org.springframework.security.taglibs.authz.AuthorizeTag

attribute: access url method  ifNotGranted  ifAllGranted  ifAnyGranted

使用方式:见SimpleDemo的index.jsp

<p>

<sec:authorize ifAllGranted="ROLE_ADMIN">#这里可以用逗号分隔,加入多个角色

你拥有管理员权限,你可以查看 该页面<a href="http://blog.163.com/sir_876/blog/admin.jsp"> 管理员进入</a> </sec:authorize> </p> <p> <sec:authorize url='/profile.jsp'>你登陆成功了可以看到 <a href="http://blog.163.com/sir_876/blog/profile.jsp"> 这个页面</a></sec:authorize>

</p>

页面标签的使用与权限配置相对应

<intercept-url            pattern="/admin.jsp"            access="hasRole('ROLE_ADMIN')" />        <intercept-url            pattern="/profile.jsp"            access="isAuthenticated()" />        <intercept-url            pattern="/**"            access="permitAll" />

对比可以看到只有ROLE_ADMIN角色的用户才能访问admin.jsp,通过认证的用户都可以访问profile.jsp

从标签源码可以知道,authorize标签判断顺序是: access->url->ifNotGranted->ifAllGranted->ifAnyGranted 但他们的关系是“与”: 即只要其中任何一个属性不满足则该标签中间的内容将不会显示给用户,举个例子:

<sec:authorize  ifAllGranted=”ROLE_ADMIN,ROLE_MEMBER” ifNotGranted=”ROLE_SUPER”>满足才会显示给用户 </sec:authorize>

标签中间的内容只有在当前用户拥有ADMIN,MEMBER角色,但不拥有SUPER权限时才会显示

access属性是基于角色判断,url属性是基于访问路径判断,与security.xml配置对应

对于ifAllGranted ,ifNotGranted,ifAnyGranted属性的理解可以与集合api类比

Collection grantedAuths  :当前用户拥有的权限
Collection requiredAuths : 当前要求的权限,即ifAllGranted ,ifNotGranted,ifAnyGranted 属性的值

满足ifAllGranted: 只需要grantedAuths.containsAll(requiredAuths);返回true即可
满足ifAnyGranted: 只需要grantedAuths.retainAll(requiredAuths);有内容即可(两集合有交集)
满足ifNotGranted:与Any相反,如果没有交集即可

二、authentication

对应的类: org.springframework.security.taglibs.authz.AuthenticationTag

attribute: property(required) var  htmlEscape  scope

使用方式:

<sec:authentication property=’name’ />
<sec:authentication property=’principal.username’ />
<sec:authentication property=’principal.enabled’ />
<sec:authentication property=’principal.accountNonLocked’ />

主要用来显示authentication属性,

var scope: 将property的值以var设置到scope域中

htmlEscape: 将特殊字符转义 > –> “&gt”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值