背景: 網上不少講配置 oauth2 ,配置方法 複雜紛繁對於初學者很不友好,讓人望而卻步
html
歡迎關注本系列博客 基於 spring cloud 最新版本 hoxton 完成oauth2 的實踐git
Spring Cloud OAuth
,用簡潔的方式搭建oauth的認證中心, 名稱 | 版本 |
---|---|
Spring Boot | 2.2.0.M5 |
Spring Cloud | Hoxton.M2 |
Spring Cloud OAuth2 | 2.2.0.M2 |
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
</dependencies>複製代碼
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
/**
* 必須注入 AuthenticationManager,否則oauth 沒法處理四種受權方式
*
* @return
* @throws Exception
*/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* 必須注入UserDetailsService ,否則oauth 密碼模式等死循環問題
*
* @return
*/
@Bean
@Override
protected UserDetailsService userDetailsService() {
InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
userDetailsManager.createUser(User.withUsername("lengleng").password("{noop}lengleng").authorities("USER").build());
return userDetailsManager;
}
}複製代碼
@Configuration
@EnableAuthorizationServer
public class BigAuthServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("appid")
.secret("{noop}secret")
.authorizedGrantTypes("password", "authorization_code", "client_credentials", "implicit", "refresh_token")
.scopes("all");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService);
}
}複製代碼
以上完成了認證服務器的功能web
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=password&username=lengleng&password=lengleng&scope=all' "http://appid:secret@localhost:8764/oauth/token"複製代碼
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
</dependencies>複製代碼
security:
oauth2:
client:
client-id: appid
client-secret: secret
scope: all
resource: # 認證中心的check_token 接口地址
token-info-uri: http://127.0.0.1:8764/oauth/check_token複製代碼
// 接入oauth2 ,聲明爲資源服務器
@EnableResourceServer
@EnableDiscoveryClient
@SpringBootApplication
public class BigUpmsServerApplication {
public static void main(String[] args) {
SpringApplication.run(BigUpmsServerApplication.class, args);
}
}複製代碼
public class BigAuthServerConfiguration extends AuthorizationServerConfigurerAdapter {
/**
* checkTokenAccess 權限設置爲isAuthenticated,否則資源服務器 來請求403
* @param oauthServer
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
oauthServer
.allowFormAuthenticationForClients()
.checkTokenAccess("isAuthenticated()");
}
}複製代碼
@RestController
public class DemoController {
@GetMapping("/info")
public Authentication authentication(Authentication authentication) {
return authentication;
}
}
複製代碼