前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >页面获取Spring Security登录用户

页面获取Spring Security登录用户

作者头像
一个会写诗的程序员
发布2018-08-17 11:29:21
1K0
发布2018-08-17 11:29:21
举报
文章被收录于专栏:一个会写诗的程序员的博客

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

代码语言:javascript
复制
${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中存的信息:

代码语言:javascript
复制
    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}

在后台获取

代码语言:javascript
复制
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()  
    .getAuthentication()  
    .getPrincipal();  
  
userDetails.getUsername()  
代码语言:javascript
复制
package com.easykotlin.reakt.controller

import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * Created by Kor on 2017-12-24 14:04:07.
 */
@RestController
@RequestMapping("/user")
class UserController {

    @GetMapping(value = ["/currentUser.json"])
    fun currentUser(): UserDto {
        val UserDto = UserDto()
        try {
            UserDto.success = true
            val loginUser = UserController.UserDto.LoginUser()
            val UserDetails = SecurityContextHolder
                .getContext()
                .authentication
                .principal
                    as UserDetails
            loginUser.username = UserDetails.username
            UserDto.loginUser = loginUser
        } catch (e: Exception) {
            UserDto.success = false
            UserDto.loginUser = UserController.UserDto.LoginUser()
            e.printStackTrace()
        }
        return UserDto;
    }

    class UserDto {
        var success = false
        lateinit var loginUser: LoginUser

        class LoginUser {
            var username = "NULL"
        }
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.12.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档