PrincipalExtractor and AuthoritiesExtractor doesn't hit

ぃ、小莉子 提交于 2019-12-25 03:22:55

问题


I have a project with Spring security and Oauth2.

On the resource server I have the following configuration:

@Configuration
public class SecurityConfiguration extends ResourceServerConfigurerAdapter {    

    @Override
    public void configure(final HttpSecurity http) throws Exception {
        http.antMatcher("/**")
            .authorizeRequests().antMatchers("/info", "/health", "/h2-console/**").permitAll()
            .anyRequest().authenticated()
            .and().headers().frameOptions().disable();
    }    

}

I have the following extractors:

@Component
public class InsurancePrincipalExtractor implements PrincipalExtractor {

    @Override
    public Object extractPrincipal(Map<String, Object> map) {
        return map.get("username");
    }

}

@Component
public class InsuranceAuthoritiesExtractor implements AuthoritiesExtractor {

   @Override
   public List<GrantedAuthority> extractAuthorities(Map<String, Object> map) {
       //Logic
}

I set the user-info-uri: http://localhost:8081/uaa/v1/me

The problem is that it does not hit my extractor methods at runtime, so nothing happens. As I know I just need to annotate it with the @Component and the Spring boot and will use it auto.

UPDATE: Solution founded. I had to add this to my configuration as well:

@Bean
    protected ResourceServerTokenServices resourceServerTokenServices(ResourceServerProperties sso,
                                                                         OAuth2ClientContext oauth2ClientContext,
                                                                          UserInfoRestTemplateFactory restTemplateFactory) {

            UserInfoTokenServices services = new UserInfoTokenServices(sso.getUserInfoUri(), sso.getClientId());
            services.setRestTemplate(restTemplateFactory.getUserInfoRestTemplate());
            services.setTokenType(sso.getTokenType());

            return services;
        }

来源:https://stackoverflow.com/questions/53206247/principalextractor-and-authoritiesextractor-doesnt-hit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!