使用spring oauth2框架獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼
使用spring oauth2框架做授權(quán)鑒定。想獲取當(dāng)前用戶信息怎么辦?
我們知道spring oauth2是基于spring security的實(shí)現(xiàn)的。
spring security可以通過(guò)SecurityContextHolder.getContext().getAuthentication().getPrincipal()獲取到當(dāng)前用戶信息。
而spring oauth2通過(guò)SecurityContextHolder.getContext().getAuthentication().getPrincipal()卻只能拿到當(dāng)前用戶的用戶名。
然而實(shí)際開(kāi)發(fā)過(guò)程中,我們較常用到的大部分都是用戶的id。
那么怎么通過(guò)配置獲取當(dāng)前用戶的信息呢?
首先我們看下,為什么oauth2通過(guò)SecurityContextHolder.getContext().getAuthentication().getPrincipal()獲取到的是用戶名?
我們看下源碼
DefaultUserAuthenticationConverter.java
public Authentication extractAuthentication(Map<String, ?> map) {
// userClaimName是靜態(tài)常量:username
if (map.containsKey(userClaimName)) {
Object principal = map.get(userClaimName);
Collection<? extends GrantedAuthority> authorities = getAuthorities(map);
// 原因就是這里。如果userDetailsService為空,返回的就是用戶名。
if (userDetailsService != null) {
UserDetails user = userDetailsService.loadUserByUsername((String) map.get(userClaimName));
authorities = user.getAuthorities();
principal = user;
}
return new UsernamePasswordAuthenticationToken(principal, "N/A", authorities);
}
return null;
}
那我們就給這個(gè)類,設(shè)置userDetailsService。
我們看下這個(gè)類的調(diào)用。
DefaultAccessTokenConverter.java
public class DefaultAccessTokenConverter implements AccessTokenConverter {
// 一開(kāi)始就被初始化好了,所以不能設(shè)置userDetailsService
private UserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
// 但是提供了setter接口,所以我們要做的就是覆蓋上面的實(shí)例。
public void setUserTokenConverter(UserAuthenticationConverter userTokenConverter) {
this.userTokenConverter = userTokenConverter;
}
}
所以如果是使用DefaultAccessTokenConverter的,代碼可以這么寫(xiě)
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore)
.authenticationManager(manager)
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
.userDetailsService(userService)
// .accessTokenConverter(tokenConverter);
DefaultAccessTokenConverter converter = new DefaultAccessTokenConverter();
DefaultUserAuthenticationConverter userAuthenticationConverter
= new DefaultUserAuthenticationConverter();
userAuthenticationConverter.setUserDetailsService(userService);
converter.setUserTokenConverter(userAuthenticationConverter);
endpoints.accessTokenConverter(converter);
}
}
如果是用jwtConverter的話,可以這么改。
定義一個(gè)accessTokenConverter繼承DefaultAccessTokenConverter
public class OauthAccessTokenConverter extends DefaultAccessTokenConverter {
public OauthAccessTokenConverter(SecurityUserService userService) {
DefaultUserAuthenticationConverter converter = new DefaultUserAuthenticationConverter();
converter.setUserDetailsService(userService);
super.setUserTokenConverter(converter);
}
}
定義一個(gè)jwtConverter繼承JwtAccessTokenConver
public class OauthJwtAccessTokenConverter extends JwtAccessTokenConverter {
public OauthJwtAccessTokenConverter(SecurityUserService userService) {
super.setAccessTokenConverter(new OauthAccessTokenConverter(userService));
}
}
注冊(cè)bean
@Configuration
public class TokenConfig {
@Bean
public TokenStore jwtTokenStore(JwtAccessTokenConverter converter) {
return new JwtTokenStore(converter);
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter(SecurityUserService userService) {
JwtAccessTokenConverter accessTokenConverter = new OauthJwtAccessTokenConverter(userService);
accessTokenConverter.setSigningKey("sign_key");
return accessTokenConverter;
}
}
Oauth2認(rèn)證服務(wù)
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager manager;
@Autowired
private SecurityUserService userService;
@Autowired
private TokenStore tokenStore;
@Autowired
private JwtAccessTokenConverter tokenConverter;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore)
.authenticationManager(manager)
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
.userDetailsService(userService)
.accessTokenConverter(tokenConverter);
}
}
最后就可以像spring security一樣。
使用SecurityContextHolder.getContext().getAuthentication().getPrincipal()獲取用戶信息了。
到此這篇關(guān)于spring oauth2獲取當(dāng)前登錄用戶信息的文章就介紹到這了,更多相關(guān)spring oauth2登錄用戶信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- spring cloud oauth2 實(shí)現(xiàn)用戶認(rèn)證登錄的示例代碼
- Spring Cloud OAuth2 實(shí)現(xiàn)用戶認(rèn)證及單點(diǎn)登錄的示例代碼
- Spring Cloud下OAUTH2注銷的實(shí)現(xiàn)示例
- Spring Cloud下基于OAUTH2認(rèn)證授權(quán)的實(shí)現(xiàn)示例
- Spring Security OAuth過(guò)期的解決方法
- Spring Security整合Oauth2實(shí)現(xiàn)流程詳解
- Spring Security OAuth2 授權(quán)碼模式的實(shí)現(xiàn)
- Spring cloud oauth2如何搭建認(rèn)證資源中心
相關(guān)文章
java idea如何根據(jù)請(qǐng)求路徑url自動(dòng)找到對(duì)應(yīng)controller方法插件
這篇文章主要介紹了java idea如何根據(jù)請(qǐng)求路徑url自動(dòng)找到對(duì)應(yīng)controller方法插件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
java web實(shí)現(xiàn)郵箱激活與忘記密碼
這篇文章主要為大家詳細(xì)介紹了java web實(shí)現(xiàn)郵箱激活與忘記密碼、重置密碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Java中如何動(dòng)態(tài)創(chuàng)建接口的實(shí)現(xiàn)方法
這篇文章主要介紹了Java中如何動(dòng)態(tài)創(chuàng)建接口的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-09-09
java一個(gè)接口多個(gè)實(shí)現(xiàn)類的調(diào)用方式
這篇文章主要給大家介紹了關(guān)于java一個(gè)接口多個(gè)實(shí)現(xiàn)類的調(diào)用方式的相關(guān)資料,經(jīng)測(cè)試確認(rèn),當(dāng)一個(gè)接口有多個(gè)實(shí)現(xiàn)時(shí),調(diào)用時(shí)只會(huì)執(zhí)行一個(gè),有時(shí)候需要多個(gè)實(shí)現(xiàn)調(diào)用,需要的朋友可以參考下2023-09-09
java虛擬機(jī)之JVM調(diào)優(yōu)詳解
這篇文章主要介紹了java虛擬機(jī)之JVM調(diào)優(yōu)詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Java虛擬機(jī)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Mybatis中resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置方式
這篇文章主要介紹了Mybatis中resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

