javascript
页面获取Spring Security登录用户
?
1.在session中取得spring security的登錄用戶名如下:
${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}
spring security 把SPRING_SECURITY_CONTEXT 放入了session 沒(méi)有直接把username 放進(jìn)去。下面一段代碼主要描述的是session中的存的變量,
存跳轉(zhuǎn)時(shí)候的URLsession
{SPRING_SECURITY_SAVED_REQUEST_KEY=SavedRequest[http://localhost:8080/AVerPortal/resourceAction/resourceIndex.action]}
存的是登錄成功時(shí)候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.在頁(yè)面端用tag獲取:
<%@ taglib prefix='security' uri='http://www.springframework.org/security/tags'%>
<security:authentication property="principal.username"></security:authentication>
或者
<security:authorize ifAllGranted="ROLE_ADMIN">
<security:authentication property="principal.username"></security:authentication>
</security:authorize>
或者取session中的值:
${session.SPRING_SECURITY_CONTEXT.authentication.principal.username}
3.在后臺(tái)獲取
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal();
userDetails.getUsername()
?
如果想要獲取更多的信息:得擴(kuò)展userDetails的默認(rèn)實(shí)現(xiàn)類user類和UserDetailsService接口
由于springsecurity是把整個(gè)user信息放入session中的即:session.SPRING_SECURITY_CONTEXT.authentication.principal。這個(gè)就是代表著user對(duì)象。
http://blog.csdn.net/zmx729618/article/details/51914836
?
6.1 th:text屬性
可對(duì)表達(dá)式或變量求值,并將結(jié)果顯示在其被包含的 html 標(biāo)簽體內(nèi)替換原有html文本
文本連接:用“+”符號(hào),若是變量表達(dá)式也可以用“|”符號(hào)
6.2 th:utext屬性
e.g.
若home.welcome=Welcome to our <b>fantastic</b> grocery store!
用<p th:text="#{home.welcome}"></p>解析結(jié)果為:
<p>Welcome to our <b>fantastic</b> grocery store!</p>
解決方案
<p th:utext="#{home.welcome}"></p>即可。
等效于<p>Welcome to our <b>fantastic</b> grocery store!</p>
http://blog.csdn.net/sun_jy2011/article/details/40215101
?
總結(jié)
以上是生活随笔為你收集整理的页面获取Spring Security登录用户的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 常见的几种RuntimeExceptio
- 下一篇: java文字特效