1.拷貝 DaoAuthenticationProvider代碼,自定義 MyAuthenticationProvider 繼承 AbstractUserDetailsAuthenticationProvider
在retrieveUser方法添加user_type
protected final UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
this.prepareTimingAttackProtection();
Map<String,String> map = (Map<String, String>) authentication.getDetails();
try {
String user_type = map.get("user_type");
UserDetails loadedUser = this.getUserDetailsService().loadUserByLoginName(username,user_type);
if (loadedUser == null) {
throw new InternalAuthenticationServiceException("UserDetailsService returned null, which is an interface contract violation");
} else {
return loadedUser;
}
} catch (UsernameNotFoundException var4) {
this.mitigateAgainstTimingAttack(authentication);
throw var4;
} catch (InternalAuthenticationServiceException var5) {
throw var5;
} catch (Exception var6) {
throw new InternalAuthenticationServiceException(var6.getMessage(), var6);
}
}
2.在WebSecurityConfigurer 內使用自定義AuthenticationManagerBuilder配置
@Bean(name="myAuthenticationProvider")
public AuthenticationProvider myAuthenticationProvider() {
MyAuthenticationProvider daoAuthenticationProvider = new MyAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(baseUserDetailsService);
daoAuthenticationProvider.setHideUserNotFoundExceptions(false);
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
return daoAuthenticationProvider;
}
@Override
public void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(myAuthenticationProvider());
}
3.在UserDetailsService基礎service 添加自定義方法 loadUserByLoginName(String username,String user_type)
在impl內根據(jù)user_type查詢用戶信息
@Override
public UserDetails loadUserByLoginName(String loginName, String userType) throws UsernameNotFoundException {
JSONResult<UserInfo> result=null;
if(SecurityConstants.USER_TYPE_ADMIN.equals(userType)){
result = userServiceApi.info(loginName, SecurityConstants.FROM_IN);
}else if(SecurityConstants.USER_TYPE_UAC.equals(userType)){
result = subscriberServiceApi.info(loginName,SecurityConstants.FROM_IN);
}
return getUserDetails(result);
}
二:在刷新token時為防止調用原來的loadUserByUsername 需要配置自定義tokenServices
1.在AuthorizationServerConfig 內添加
private CustomTokenServices tokenServices(AuthorizationServerEndpointsConfigurer endpoints) {
CustomTokenServices tokenServices = new CustomTokenServices();
tokenServices.setTokenStore(tokenStore());
tokenServices.setSupportRefreshToken(true);
tokenServices.setReuseRefreshToken(true);
tokenServices.setAccessTokenValiditySeconds(-1);
tokenServices.setClientDetailsService(endpoints.getClientDetailsService());
tokenServices.setTokenEnhancer(endpoints.getTokenEnhancer());
addUserDetailsService(tokenServices, baseUserDetailsService);
return tokenServices;
}
private void addUserDetailsService(CustomTokenServices tokenServices, BaseUserDetailsService userDetailsService) {
if (userDetailsService != null) {
PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();
provider.setPreAuthenticatedUserDetailsService(new MyUserDetailsByNameServiceWrapper(userDetailsService));
tokenServices.setAuthenticationManager(new ProviderManager(Arrays.asList(provider)));
}
}
###############添加endpoints.tokenServices###########
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
.tokenStore(tokenStore())
.tokenEnhancer(tokenEnhancer())
.tokenServices(tokenServices(endpoints))
.userDetailsService(baseUserDetailsService)
.authenticationManager(authenticationManager)
.reuseRefreshTokens(false)
.exceptionTranslator(new CustomWebResponseExceptionTranslator());
}
自定義 UserDetailsByNameServiceWrapper
public class MyUserDetailsByNameServiceWrapper<T extends Authentication> implements AuthenticationUserDetailsService<T>, InitializingBean {
private BaseUserDetailsService userDetailsService = null;
public MyUserDetailsByNameServiceWrapper() {
}
public MyUserDetailsByNameServiceWrapper(BaseUserDetailsService userDetailsService) {
Assert.notNull(userDetailsService, "userDetailsService cannot be null.");
this.userDetailsService = userDetailsService;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(this.userDetailsService, "UserDetailsService must be set");
}
public UserDetails loadUserDetails(T authentication) throws UsernameNotFoundException {
AbstractAuthenticationToken principal = (AbstractAuthenticationToken) authentication.getPrincipal();
Map<String,String> map = (Map<String, String>) principal.getDetails();
String user_type = map.get("user_type");
return this.userDetailsService.loadUserByLoginName(authentication.getName().split("@")[1],user_type);
}
public void setUserDetailsService(BaseUserDetailsService aUserDetailsService) {
this.userDetailsService = aUserDetailsService;
}
}
自定義 DefaultTokenServices 拷貝
public class CustomTokenServices implements AuthorizationServerTokenServices, ResourceServerTokenServices, ConsumerTokenServices, InitializingBean {
......拷貝DefaultTokenServices代碼
登錄方式
http://localhost:9999/auth/oauth/token?username=admin&randomCode=g7a4&randomStr=452f9aa0-7dd2-4615-a53b-6c3d5be3b36b&grant_type=password&user_type=admin&password=1234561
三 細節(jié)處理:
1.原 MobileAuthFilter 的setDetails 需要自己封裝參數(shù)
private void setDetails(HttpServletRequest request,
MobileAuthToken authRequest) {
Map<String,String> map = new LinkedHashMap<String, String>();
Enumeration paramNames = request.getParameterNames();
while (paramNames.hasMoreElements()){
String paramName = (String) paramNames.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() != 0) {
map.put(paramName, paramValue);
}
}
}
authRequest.setDetails(map);
}
解決oauth2多類型用戶登錄
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門买决,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沛婴,“玉大人,你說我怎么就攤上這事督赤∴业疲” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵够挂,是天一觀的道長旁仿。 經(jīng)常有香客問我,道長孽糖,這世上最難降的妖魔是什么枯冈? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮办悟,結果婚禮上尘奏,老公的妹妹穿的比我還像新娘。我一直安慰自己病蛉,他們只是感情好炫加,可當我...
- 文/花漫 我一把揭開白布瑰煎。 她就那樣靜靜地躺著,像睡著了一般俗孝。 火紅的嫁衣襯著肌膚如雪酒甸。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼盛卡,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了筑凫?” 一聲冷哼從身側響起滑沧,我...
- 正文 年R本政府宣布嘱吗,位于F島的核電站玄组,受9級特大地震影響,放射性物質發(fā)生泄漏谒麦。R本人自食惡果不足惜俄讹,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望绕德。 院中可真熱鬧患膛,春花似錦、人聲如沸耻蛇。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至跃捣,卻和暖如春漱牵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背枝缔。 一陣腳步聲響...