Spring Security Oauth2 JWT适用于.properties,但不适用于.yml

时间:2018-08-24 10:17:13

标签: java spring-boot spring-security spring-security-oauth2 properties-file

我正在使用SpringBoot,遇到了非常奇怪的问题。我有资源服务器,它可以与application.properties一起正常工作,但是当我尝试将.properties迁移到.yml时 当我将带有JWT令牌的请求发送到我的一个控制器时,出现UNATHORIZED错误。

application.yml

security:
  oauth2:
    resource:
      id: ouath2_id

application.properties

security.oauth2.resource.id=oauth2_id

ResourceConfig类

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@EnableResourceServer
public class ResourceConfig extends ResourceServerConfigurerAdapter {

    @Value("${security.oauth2.resource.id}")
    private String resourceId;

    private String publicKey = "xxx";

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        System.out.println("RESORCEID: " + resourceId); //IT IS LOADING CORRECTLY
        resources
                .resourceId(resourceId)
                .tokenServices(tokenServices())
                .tokenStore(tokenStore())
        .authenticationEntryPoint(customAuthEntryPoint())
        .accessDeniedHandler(customDeniedHandler());
    }
    @Bean
    public AuthenticationEntryPoint customAuthEntryPoint(){
        return new AuthFailureHandler();
    }

    @Bean
    public AccessDeniedHandler customDeniedHandler(){
        return new AccessDeniedHandlerImpl();
    }


    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .anonymous().disable()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
        .antMatchers("/api/**").authenticated();
    }


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


    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setVerifierKey(publicKey);
        return converter;
    }
    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        defaultTokenServices.setTokenEnhancer(accessTokenConverter());
        return defaultTokenServices;
    }
}

我检查了一下,加载窗体属性的唯一属性是resourceId,并且正在获得所需的值。

所以唯一的区别是.yml而不是.properties

我不知道是什么问题。

0 个答案:

没有答案