簡體   English   中英

Spring Security-實現OAuth2 SSO

[英]Spring security - implement oauth2 sso

我想用spring security和oauth2 sso實現中央身份驗證系統 換句話說,我有一個負責授權的spring boot應用程序和一個簡單的客戶端。 我的客戶有rest API。 首先,我從授權服務器獲取令牌,然后將請求發送到客戶端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:

ApplicationConfig:

@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

首先,我將client_id和密碼以及用戶名,密碼和grant_type一起發送到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發送了一個請求。 但是似乎客戶端應用程序會忽略標題,並顯示服務器登錄頁面作為結果。 我該如何解決這個問題?

@EnableOAuth2Sso是用於將OAuth 2.0用作最終用戶身份驗證機制的注釋(例如,“使用Google登錄”按鈕)。 此注釋鏈接您的應用程序以重定向到授權服務器上的登錄頁面,您將在該頁面上登錄然后重定向回您的應用程序。

如果這是您的意圖,那么您需要更新授權服務器以支持authorization_code授予流程,而不是password授予流程。

但是,如果您的客戶端嚴格來說是REST API,則您更有可能需要使用@EnableResourceServer而不是@EnableOAuth2Sso@EnableResourceServer客戶端。 資源服務器通過授權HTTP標頭將令牌作為授權。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM