[英]Grails 3, Spring Security Rest username after login
I have a Grails 3 application with Spring Security Rest implemented, I can authenticate from my Angular2 app successfully, and with @Secured() applied to the controllers can control which areas of the application can be accessed. 我有一个实现了Spring Security Rest的Grails 3应用程序,我可以从Angular2应用程序成功进行身份验证,并且将@Secured()应用于控制器可以控制可以访问应用程序的哪些区域。 So given this, I know that the authentication and authorization is working.
因此,鉴于此,我知道身份验证和授权正在起作用。
I'm trying to retrieve the logged in user's username that was used to authenticate. 我正在尝试检索用于身份验证的登录用户的用户名。
I've tried Authentication authentication = SecurityContextHolder.getContext().getAuthentication()
it's null. 我已经尝试过
Authentication authentication = SecurityContextHolder.getContext().getAuthentication()
它为null。
I tried Principal principal = request.getUserPrincipal()
it's also null. 我尝试了
Principal principal = request.getUserPrincipal()
它也为null。
Here's my application.groovy 这是我的应用程序。
grails.plugin.springsecurity.providerNames = ['customADAuthProvider']
grails.plugin.springsecurity.successHandler.alwaysUseDefault = true
grails.plugin.springsecurity.userLookup.userDomainClassName = 'User'
grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'UserRole'
grails.plugin.springsecurity.authority.className = 'Role'
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
[pattern: '/', access: ['permitAll']],
[pattern: '/error', access: ['permitAll']],
[pattern: '/index', access: ['permitAll']],
[pattern: '/index.gsp', access: ['permitAll']],
[pattern: '/shutdown', access: ['permitAll']],
[pattern: '/assets/**', access: ['permitAll']],
[pattern: '/**/js/**', access: ['permitAll']],
[pattern: '/**/css/**', access: ['permitAll']],
[pattern: '/**/images/**', access: ['permitAll']],
[pattern: '/**/favicon.ico', access: ['permitAll']]
]
grails.plugin.springsecurity.filterChain.chainMap = [
[pattern: '/assets/**', filters: 'none'],
[pattern: '/**/js/**', filters: 'none'],
[pattern: '/**/css/**', filters: 'none'],
[pattern: '/**/images/**', filters: 'none'],
[pattern: '/**/favicon.ico', filters: 'none'],
[pattern: '/api/**', filters: 'JOINED_FILTERS,-anonymousAuthenticationFilter,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter,-rememberMeAuthenticationFilter']
]
grails.plugin.springsecurity.rest.logout.endpointUrl = '/api/logout'
No matter what I try, I can never get the authentication object or principal object to retrieve the username or any user details. 无论我做什么,我都永远无法获得身份验证对象或主体对象来检索用户名或任何用户详细信息。
I'm using spring-security-rest:2.0.0.RC1
, and Grails 3.3.6
我正在使用
spring-security-rest:2.0.0.RC1
和Grails 3.3.6
You can do something like this... 你可以做这样的事情...
class SomeController {
def springSecurityService
def someAction() {
def user = springSecurityService.currentUser
// ...
}
}
Or this... 或这个...
class SomeController {
def springSecurityService
def someAction() {
def principal = springSecurityService.principal
String username = principal.username
def authorities = principal.authorities // a Collection of GrantedAuthority
boolean enabled = principal.enabled
// ...
}
}
Both of those are pasted directly from https://grails-plugins.github.io/grails-spring-security-core/3.2.x/index.html 两者都直接从https://grails-plugins.github.io/grails-spring-security-core/3.2.x/index.html粘贴
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.