Token information Token is returned to be generated by Oauth2 after loginning found. But cannot make Token ClientDetailsService target all the time after test and verify is successful,be empty all the time do not know what returns a responsibility. Who has been encountered? Seek solution.
SecurityConfig.java configuration
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.headers()
.frameOptions()
.disable();
http.authorizeRequests()
.antMatchers(\"/actuator/**\",
\"/oauth/removeToken\",
\"/oauth/delToken/*\",
\"/oauth/listToken\",
\"/mobile/**\").permitAll()
.anyRequest()
.authenticated();
http.exceptionHandling()
.accessDeniedHandler(new DreamAccessDeniedHandler(\"/accessDenied\"));
http.formLogin()
.loginPage(\"/login\")
// .loginProcessingUrl(\"/session\")
.loginProcessingUrl(\"/auth/oauth/token\")
.failureHandler(authFailureEvenHandler)
.successHandler(authSuccessEvenHandler)
.authenticationDetailsSource(authDetailsSource)
.permitAll();
http.rememberMe()
.tokenRepository(rememberMeTokenRepository())
.userDetailsService(userDetailsService)
.tokenValiditySeconds(30 * 24 * 60);
http.logout()
.logoutRequestMatcher(new AntPathRequestMatcher(\"/logout\"))
.logoutSuccessUrl(\"/\")
.deleteCookies(\"JSESSIONID\", \"remember-me\");
http.csrf()
.ignoringAntMatchers(\"/druid/**\", \"/ueditor\")
.csrfTokenRepository(new CookieCsrfTokenRepository())
.disable();
// @formatter:on
}
Is Debug of code of DreamAuthSuccessEvenHandler.java AuthSuccessEvenHandler specific implementation prints content to be nodded for empty problem in System.out.println(\” ClientDetailsService \”+clientDetailsService); why be all the time empty?
@Slf4j
@Component
@RequiredArgsConstructor
//@Builder
public class DreamAuthSuccessEvenHandler implements AuthenticationSuccessHandler {
// private final ApplicationEventPublisher publisher;
// private final MessageSource messageSource;
private static final String BASIC_ = \"Basic \";
private ObjectMapper objectMapper;
private PasswordEncoder passwordEncoder;
private ClientDetailsService clientDetailsService;
private PigClientDetailsService pigClientDetailsService;
private AuthorizationServerTokenServices defaultAuthorizationServerTokenServices;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
// WebUtil.renderJson(response, R.success());
// Ji Lu Deng Lu Ri Zhi
SysLogEvent sysLogEvent = SysLogUtils.getSysLogDTO();
sysLogEvent.setOperation(\" Deng Lu Cheng Gong \");
sysLogEvent.setClassMethod(\"net.dreamlu.secrity.auth.DreamAuthHandler.onAuthenticationSuccess();\");
// Fa Song spring event Shi Jian
// publisher.publishEvent(sysLogEvent);
log.info(\" Yong Hu :{} Deng Lu Cheng Gong \", authentication.getPrincipal());
// R<String> result = R.success(\" Deng Lu Cheng Gong \");
// WebUtil.renderJson(response, result);
String header = request.getHeader(HttpHeaders.AUTHORIZATION);
if (header == null || !header.startsWith(BASIC_)) {
throw new UnapprovedClientAuthenticationException(\" Qing Qiu Tou Zhong client Xin Xi Wei Kong \");
}
try {
String[] tokens = AuthUtils.extractAndDecodeHeader(header);
assert tokens.length == 2;
String clientId = tokens[0];
System.out.println(\" clientId \"+clientId);
System.out.println(\" clientDetailsService \"+clientDetailsService);
ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
// Xiao Yan secret
if (!passwordEncoder.matches(tokens[1], clientDetails.getClientSecret())) {
throw new InvalidClientException(\"Given client ID does not match authenticated client\");
}
TokenRequest tokenRequest = new TokenRequest(MapUtil.newHashMap(), clientId, clientDetails.getScope(), \"mobile\");
// Xiao Yan scope
new DefaultOAuth2RequestValidator().validateScope(tokenRequest, clientDetails);
OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);
OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);
OAuth2AccessToken oAuth2AccessToken = defaultAuthorizationServerTokenServices.createAccessToken(oAuth2Authentication);
log.info(\" Huo Qu token Cheng Gong :{}\", oAuth2AccessToken.getValue());
response.setCharacterEncoding(CharsetUtil.UTF_8);
response.setContentType(CommonConstants.CONTENT_TYPE);
// PrintWriter printWriter = response.getWriter();
// printWriter.append(objectMapper.writeValueAsString(oAuth2AccessToken));
WebUtil.renderJson(response, objectMapper.writeValueAsString(oAuth2AccessToken));
} catch (IOException e) {
throw new BadCredentialsException(
\"Failed to decode basic authentication token\");
}
}
/**
* https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-updated
* Encoded password does not look like BCrypt
*
* @return PasswordEncoder
*/
@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
}