17

我下面来自Dave Syer基本春天启动的OAuth2例如:https://github.com/dsyer/sparklr-boot/blob/master/src/main/java/demo/Application.java如何让Spring引导和的OAuth2例如使用其他的密码授予证书不是默认

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
@RestController 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 

    @RequestMapping("/") 
    public String home() { 
     return "Hello World"; 
    } 

    @Configuration 
    @EnableResourceServer 
    protected static class ResourceServer extends ResourceServerConfigurerAdapter { 

     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      // @formatter:off 
      http 
       // Just for laughs, apply OAuth protection to only 2 resources 
       .requestMatchers().antMatchers("/","/admin/beans").and() 
       .authorizeRequests() 
       .anyRequest().access("#oauth2.hasScope('read')"); 
      // @formatter:on 
     } 

     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
      resources.resourceId("sparklr"); 
     } 

    } 

    @Configuration 
    @EnableAuthorizationServer 
    protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { 

     @Autowired 
     private AuthenticationManager authenticationManager; 

     @Override 
     public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
      endpoints.authenticationManager(authenticationManager); 
     } 

     @Override 
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
      // @formatter:off 
      clients.inMemory() 
       .withClient("my-trusted-client") 
        .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit") 
        .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT") 
        .scopes("read", "write", "trust") 
        .resourceIds("sparklr") 
        .accessTokenValiditySeconds(60) 
      .and() 
       .withClient("my-client-with-registered-redirect") 
        .authorizedGrantTypes("authorization_code") 
        .authorities("ROLE_CLIENT") 
        .scopes("read", "trust") 
        .resourceIds("sparklr") 
        .redirectUris("http://anywhere?key=value") 
      .and() 
       .withClient("my-client-with-secret") 
        .authorizedGrantTypes("client_credentials", "password") 
        .authorities("ROLE_CLIENT") 
        .scopes("read") 
        .resourceIds("sparklr") 
        .secret("secret"); 
     // @formatter:on 
     } 

    } 
} 

的例子都非常好,这两种类型的授权,但密码授权使用Spring Boot默认安全用户(在启动过程中回显为“使用默认安全密码:927ca0a0-634a-4671-bd1c-1323a866618a”)。

我的问题是你如何覆盖默认的用户帐户,实际上依赖WebSecurityConfig?我已经添加了这样一个部分:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
protected static class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) 
      throws Exception { 
     authManagerBuilder.inMemoryAuthentication().withUser("user") 
       .password("password").roles("USER"); 
    } 
} 

但它似乎并没有重写默认的Spring用户/密码,即使文档建议它应该。

我错过了什么让它工作?

+0

没有它不应该,除非你加上'@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)'它。您可以通过设置属性'security.user.name'和'security.user.password'来设置application.properties文件中的默认用户名/密码。有关更多属性,请参见[参考指南](http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)。 – 2014-10-29 07:05:11

+0

这里有一个更好的示例(更新):https://github.com/spring-projects/spring-security-oauth/blob/master/tests/annotation/jdbc/src/main/java/demo/Application的.java#L81。这个'authenticationManager'方法是2.0.4快照中的新覆盖(如果你想在2.0.3中使用它,请查看实现)。 – 2014-10-29 08:03:53

+0

@DaveSyer样本没有运行我,“创建名称为'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration'的错误' – 2015-01-08 03:57:26

回答

7
@Configuration 
protected static class AuthenticationManagerConfiguration extends GlobalAuthenticationConfigurerAdapter { 

     @Override 
     public void init(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication().withUser("min").password("min").roles("USER"); 
     } 

    } 
+0

是的,这比我在2.0.3中使用的更好。 – 2015-01-05 20:02:29

6

正如我仍然在2.0.3,我尝试了一些更多的东西,这似乎是工作:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
protected static class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception { 
     authManagerBuilder 
      .inMemoryAuthentication() 
       .withUser("user1").password("password1").roles("USER").and() 
       .withUser("admin1").password("password1").roles("ADMIN"); 
    } 

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

通过明确定义的AuthenticationManager豆,内置的用户认证走了,它开始依靠我自己的inMemoryAuthentication。当2.0.4发布时,我会重新评估Dave在上面发布的解决方案,因为它看起来会更优雅。

+0

我认为当您需要在OAuth服务器中配置静态页面并且需要公开时,此解决方案更适合。你可以重写'configure(HttpSecurity http)'和'configure(WebSecurity web)'并为此定义特定的'antMatchers'。 – 2015-09-10 16:31:36

+0

您是否知道如何将用户存储在数据库中并在此时检查他们的凭据? – Marcel 2016-02-27 20:54:48

+0

干杯!上面的解决方案很有用@s​​hawn – Harleen 2016-07-20 10:47:26

3

的例子指出以上 -
https://github.com/dsyer/sparklr-boot/blob/master/src/main/java/demo/Application.java
为春季1.3

如果使用Spring 1.5及以上(其通常将是现在的情况下)需要添加额外的属性。

正如其他人所指出的,我们可以使用

@Configuration 
@EnableWebSecurity 
public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter { 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers("/resources/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/user/getEmployeesList") 
      .hasAnyRole("ADMIN").anyRequest().authenticated().and().formLogin() 
      .permitAll().and().logout().permitAll(); 

     http.csrf().disable(); 
    } 

    @Override 
    public void configure(AuthenticationManagerBuilder authenticationMgr) throws Exception { 
     authenticationMgr.inMemoryAuthentication().withUser("javainuse").password("javainuse") 
      .authorities("ROLE_ADMIN"); 
    } 
} 

重要的一点是,如果使用Spring启动1.5及以上还需要添加以下属性 -

security.oauth2.resource.filter-order = 3 

面临诸多问题试图识别这一点。 还发现上述问题陈述了很好的借鉴 - Spring Boot + OAuth2 Example

相关问题