如何在Spring Boot Security OAuth2应用程序中仅为某些类启用OAuth2?

时间:2018-05-19 01:54:34

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

我按照本指南添加了OAuth2

https://spring.io/guides/tutorials/bookmarks/#_securing_a_rest_service

但现在需要对每一页进行身份验证!

$ curl -s http://localhost:8080/login 
{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

我只想在一个特定的类中使用API​​上的OAuth2。我试过阅读Spring Boot的参考资料

https://docs.spring.io/spring-boot/docs/1.5.14.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-security

但它根本没用!它给出了0个例子。

  

要自定义它,通常使用WebSecurityConfigurerAdapter类型的外部属性和bean(例如,添加基于表单的登录)。
  以上所有内容都可以使用外部属性(安全性。*)打开或关闭或修改。要在不更改任何其他自动配置功能的情况下覆盖访问规则,请使用@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)添加类型为WebSecurityConfigurerAdapter的@Bean,并对其进行配置以满足您的需求。

如何自定义它?! 哪个属性?! 如何配置以满足您的需求?!

我尝试设置application.properties

security.basic.enabled = false
security.ignored = /login

但它仍然需要OAuth2身份验证。我只想为课程IShortUrlApiInteface启用OAuth2,并为所有其他@Controllers启用它。

2 个答案:

答案 0 :(得分:1)

创建类,扩展ResourceServerConfigurerAdapter,并覆盖方法configure(HttpSecurity)

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    String [] ignoredPaths = new String[]{
            "/robots.txt", "/error", "/login", "/doLogut", "/home", "/pageNotFound", 
            "/css/**", "/js/**", "/fonts/**", "/img/**", ...
    };

    @Override
    public void configure(HttpSecurity http) throws Exception{

        http.authorizeRequests()
            .antMatchers(ignoredPaths).permitAll()
            .anyRequest().authenticated()
        .and()
            .httpBasic();   
    }

答案 1 :(得分:-1)

在您的情况下,您需要设置登录URI的权限。 如果您使用的是微服务架构,则需要在每个项目/服务中创建资源配置。

  • security.oauth2.resource.user-info-uri定义授权服务URI
  • clientId定义了您的实际客户端ID
  • 在oauth中为每个资源服务器需要创建唯一的resource-id 以下是资源服务器的示例。

    @Configuration     @EnableResourceServer
        公共类ResourceConfig扩展ResourceServerConfigurerAdapter {         @value(" $ {security.oauth2.resource.user-INFO-URI}&#34)         private String userInfoUri;

    @Value("${client-id}")
    private String clientId;
    
    @Override
    public void configure(final ResourceServerSecurityConfigurer resources) throws Exception {
        resources.resourceId("ig-user");
    }
    
    @Override
    public void configure(final HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/api/v1/user/activate/**").permitAll()//
                .antMatchers("/api/v1/user/access/check/**").permitAll()//
                .antMatchers("/api/v1/profile/exists/**").permitAll()//
                .antMatchers("/api/v1/user/sign-up/**").permitAll()//
                .antMatchers("/download/**").permitAll()//
                .anyRequest().authenticated();
    }
    
    @Primary
    @Bean
    public UserInfoTokenServices tokenService() {
        final UserInfoTokenServices tokenService = new UserInfoTokenServices(userInfoUri, clientId);
        return tokenService;
    }
    

    }

相关问题