代码之家  ›  专栏  ›  技术社区  ›  hamed

Spring安全性-实现oauth2 sso

  •  1
  • hamed  · 技术社区  · 7 年前

    具有spring安全性和oauth2 sso的中央认证系统 . 换句话说,我有一个负责授权的spring引导应用程序和一个简单的客户端。我的客户机有RESTAPI。首先,我从授权服务器获取令牌,然后向客户端API发送一个请求,其中授权头包含来自上述请求的承载令牌。但是这个请求总是让我感动 服务器登录页

    Server

    AuthorizationServerConfig.java

    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    
    @Autowired
    private AuthenticationManager authenticationManager;
    
    
    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        oauthServer
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
    }
    
    
    
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("SampleClientId")
                .secret("{noop}secret")
                .authorizedGrantTypes("password")
                .scopes("user_info")
                .autoApprove(true);
    }
    
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(this.authenticationManager);
    }
    

    ApplicationConfig:

    @SpringBootApplication
    @EnableResourceServer
    public class ApplicationConfig extends SpringBootServletInitializer {
    
    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class, args);
    }
    
    }
    

    SecurityConfig:

    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //this is just example
        auth.inMemoryAuthentication().withUser("user").password("{noop}1234").roles("user");
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers()
                .antMatchers("/login", "/oauth/authorize", "/oauth/token")
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin().permitAll();
    
    }
    
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    

    application.yml:

    server:
      port: 8900
      servlet:
        context-path: /auth
    

    Client:

    应用程序配置:

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

    SecurityConfig:

    @Configuration
    @EnableOAuth2Sso
    public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/", "/login**")
                .permitAll()
                .anyRequest()
                .authenticated();
    }
    }
    

    TestController:

    @RestController
    public class HomeController {
    
    @GetMapping("/")
    public String index() {
        return "home";
    }
    
    @RequestMapping("/admin")
    public String admin() {
        return "admin";
    }
    }
    

    application.yml:

    server:
      port: 9000
      servlet:
        context-path: /client1
    security:
      basic:
        enabled: false
      oauth2:
        client:
          clientId: SampleClientId
          clientSecret: secret
          accessTokenUri: http://localhost:8900/auth/oauth/token
          userAuthorizationUri: http://localhost:8900/auth/oauth/authorize
        resource:
          userInfoUri: http://localhost:8900/auth/user/me
    

    localhost:8900/auth/oauth/token

    {
      "access_token": "603b505f-e701-43d0-b8b8-976a2178f7ea",
      "token_type": "bearer",
      "expires_in": 43199,
      "scope": "user_info"
    }
    

    现在,我拿起上面的令牌并向 localhost:9000/client1/admin with标头包含上述标记。但客户端应用程序似乎忽略了标题,并显示了服务器登录页面。我如何解决这个问题?

    1 回复  |  直到 7 年前
        1
  •  3
  •   jzheaux Andrii Pischanski    7 年前

    @EnableOAuth2Sso 是将OAuth 2.0用作最终用户身份验证机制的注释(例如“使用Google登录”按钮)。此批注连接应用程序以重定向到授权服务器上的登录页面,您将在该页面上登录,然后重定向回应用程序。

    update your Authorization Server to support the authorization_code grant flow 而不是 password 赠款流动。

    但是,如果您的客户机严格来说是一个RESTAPI,那么您更可能需要使用 @EnableResourceServer 而不是 . A Resource Server