如何从用户名获取访问令牌?

时间:2019-01-07 09:32:55

标签: spring-boot spring-oauth2

我有一个项目,该项目具有JWT令牌的自定义实现,并且可以通过调用方法从集成测试中获取访问令牌:

private void addTokenToRequestHeader(HttpHeaders headers, String username) {
  tokenAuthenticationService.addAccessTokenToHeader(headers, username);
}

现在,我将安全性更改为使用OAuth2,并且不再使用自定义JWT实现进行配置:

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
    // The client id and client secret
    .withClient(OAUTH_CLIENT_ID)
    .secret(OAUTH_CLIENT_SECRET)
    // The endpoint at the client application to redirect to
    .redirectUris(OAUTH_CLIENT_URL)
    // The type of request the authorization server expects for the client
    .authorizedGrantTypes(OAUTH_GRANT_TYPE_PASSWORD, OAUTH_GRANT_TYPE_AUTHORIZATION_CODE, OAUTH_GRANT_TYPE_REFRESH_TOKEN)
    // The permissions the client needs to send requests to the authorization server
    .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
    // The resources server id
    .resourceIds(RESOURCE_SERVER_ID)
    // The scope of content offered by the resources servers
    .scopes("read_profile", "write_profile", "read_firstname")
    // The lifespan of the tokens for the client application
    .accessTokenValiditySeconds(jwtProperties.getAccessTokenExpirationTime())
    .refreshTokenValiditySeconds(jwtProperties.getRefreshTokenExpirationTime());
}

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    security
    .tokenKeyAccess("permitAll()")
    .checkTokenAccess("isAuthenticated()")
    .passwordEncoder(oauthClientPasswordEncoder);
}

@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints
    .authenticationManager(authenticationManager)
.tokenServices(defaultTokenServices())
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
.tokenEnhancer(jwtAccessTokenConverter())
.accessTokenConverter(jwtAccessTokenConverter())
    .userDetailsService(userDetailsService);
}

@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
  JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
  jwtAccessTokenConverter.setKeyPair(new KeyStoreKeyFactory(new ClassPathResource(jwtProperties.getSslKeystoreFilename()), jwtProperties.getSslKeystorePassword().toCharArray()).getKeyPair(jwtProperties.getSslKeyPair()));
  return jwtAccessTokenConverter;
}

@Bean
public TokenEnhancer tokenEnhancer() {
    return new CustomTokenEnhancer();
}

// Add user information to the token
class CustomTokenEnhancer implements TokenEnhancer {

  @Override
  public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    User user = (User) authentication.getPrincipal();
    Map<String, Object> info = new LinkedHashMap<String, Object>(accessToken.getAdditionalInformation());
    info.put(CommonConstants.JWT_CLAIM_USER_EMAIL, user.getEmail().getEmailAddress());
    info.put(CommonConstants.JWT_CLAIM_USER_FULLNAME, user.getFirstname() + " " + user.getLastname());
    info.put("scopes", authentication.getAuthorities().stream().map(s -> s.toString()).collect(Collectors.toList()));
    info.put("organization", authentication.getName());
    DefaultOAuth2AccessToken customAccessToken = new DefaultOAuth2AccessToken(accessToken);
    customAccessToken.setAdditionalInformation(info);
    customAccessToken.setExpiration(tokenAuthenticationService.getExpirationDate());
    return customAccessToken;
  }

}

@Bean
public TokenStore tokenStore() {
  return new JwtTokenStore(jwtAccessTokenConverter());
}

@Bean
@Primary
public DefaultTokenServices defaultTokenServices() {
  DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
  defaultTokenServices.setTokenStore(tokenStore());
  defaultTokenServices.setSupportRefreshToken(true);
  return defaultTokenServices;
}

及其随附的安全配置:

@Bean
public UserDetailsService userDetailsService() {
  return new UserDetailsServiceImpl();
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
  return super.authenticationManagerBean();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  auth.userDetailsService(userDetailsService).passwordEncoder(userPasswordEncoder);
}

// Allow preflight requests
@Override
public void configure(WebSecurity web) throws Exception {
  web.ignoring().antMatchers(HttpMethod.OPTIONS, "/**");
}

现在,要获取访问令牌,我必须发送一个请求:

@Before
public void setup() throws Exception {
  super.setup();

  addTokenToRequestHeader(httpHeaders, ClientFixtureService.CLIENT_ID, UserFixtureService.USER_EMAIL, UserFixtureService.USER_PASSWORD);
}

private void addTokenToRequestHeader(HttpHeaders headers, String oauthClientId, String username, String password) throws Exception {
  String token = getOAuthAccessToken(oauthClientId, username, password);
  headers.remove(CommonConstants.ACCESS_TOKEN_HEADER_NAME);
  headers.add(CommonConstants.ACCESS_TOKEN_HEADER_NAME, tokenAuthenticationService.buildOAuthAccessToken(token));
}

private void addBase64UserPasswordHeaders(String username, String password, HttpHeaders httpHeaders) {
  httpHeaders.setContentType(MediaType.APPLICATION_JSON);
  httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
  String usernamePassword = username + ":" + password;
  String encodedAuthorisation = Base64.getEncoder().encodeToString(usernamePassword.getBytes(UTF_8));
  httpHeaders.add(CommonConstants.ACCESS_TOKEN_HEADER_NAME,
      CommonConstants.AUTH_BASIC + " " + new String(encodedAuthorisation));
}

private String getOAuthAccessToken(String oauthClientId, String username, String password) throws Exception {
  MultiValueMap<String, String> oauthParams = new LinkedMultiValueMap<>();
  oauthParams.add("grant_type", AuthorizationServerConfiguration.OAUTH_GRANT_TYPE_PASSWORD);
  oauthParams.add("client_id", oauthClientId);
  oauthParams.add("username", username);
  oauthParams.add("password", password);
  addBase64UserPasswordHeaders(AuthorizationServerConfiguration.OAUTH_CLIENT_ID, AuthorizationServerConfiguration.OAUTH_CLIENT_SECRET, httpHeaders);
  ResultActions mvcResult = this.mockMvc
      .perform(post(RESTConstants.SLASH + DomainConstants.AUTH + RESTConstants.SLASH + DomainConstants.TOKEN)
      .headers(httpHeaders)
      .params(oauthParams)
      .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
      .andDo(print())
      .andExpect(status().isOk());
  String resultString = mvcResult.andReturn().getResponse().getContentAsString();
  JacksonJsonParser jsonParser = new JacksonJsonParser();
  return jsonParser.parseMap(resultString).get("access_token").toString();
}

但是在运行调试器时,永远不会调用我的用户详细信息实现loadUserByUsername方法。在我具有JWT令牌的自定义实现且没有OAuth2配置之前,就调用了它:

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

  @Autowired
  private CredentialsService credentialsService;

  @Override
  @Transactional
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    if (username != null && !username.isEmpty()) {
      try {
        User user = credentialsService.findByEmail(new EmailAddress(username));
        return new UserDetailsWrapper(user);
      } catch (EntityNotFoundException e) {
        throw new UsernameNotFoundException("The user " + username + " was not found.");
      }
    }
    throw new UsernameNotFoundException("The user " + username + " was not found.");
  }

}

我的请求如下:

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /auth/token
       Parameters = {grant_type=[password], client_id=[ng-xxx], username=[xxx@yahoo.xx], password=[xxxx]}
          Headers = {Content-Type=[application/json, application/json], Accept=[application/json], Authorization=[Basic bmctemxxbzpzZWNyZXQ=]}
             Body = <no character encoding set>

这是控制台日志必须说的:

2019-01-08 09:08:11.841 DEBUG 18338 --- [           main] o.s.s.w.a.www.BasicAuthenticationFilter  : Authentication request for failed: org.springframework.security.authentication.BadCredentialsException: Bad credentials
2019-01-08 09:08:11.842 DEBUG 18338 --- [           main] s.w.a.DelegatingAuthenticationEntryPoint : Trying to match using RequestHeaderRequestMatcher [expectedHeaderName=X-Requested-With, expectedHeaderValue=XMLHttpRequest]
2019-01-08 09:08:11.842 DEBUG 18338 --- [           main] s.w.a.DelegatingAuthenticationEntryPoint : No match found. Using default entry point org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint@6cb84986

1 个答案:

答案 0 :(得分:0)

当您尝试使用带有基本凭据的/oauth/token标头发布到Authorization时,会出现以下异常。这样就不会调用您的UserDetailsService.loadUserByUsername方法,这意味着您的OAuth2客户端凭据ng-zlqo:secret未在OAuth2 AuthorizationServer中注册。

o.s.s.w.a.www.BasicAuthenticationFilter  : Authentication request for failed: org.springframework.security.authentication.BadCredentialsException: Bad credentials