最近公司系統重構,須要提供API接口給其餘部門調用,因爲架構緣由,這些API有可能會被外部訪問,基於安全性的考慮,決定使用OAuth來保護這些API,以避免被隨意調用。html
因爲系統衆多,不可能在每一個系統中都配置OAuth認證受權功能,所以須要構建一個獨立的OAuth服務器,專門負責認證受權,這裏採用的框架是Spring Boot。java
整個認證受權流程中有三個角色:web
受權模式有四種:spring
具體定義可看理解 OAuth 2.0json
由於訪問OAuth服務器的都是公司內部系統,而且不可能使用同一個登陸頁面,因此只有密碼模式適用,所以後面配置的時候也只配置密碼模式。安全
具體流程以下圖服務器
下面開始實現一個簡單版的OAuth服務器架構
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
@SpringBootApplication @EnableAuthorizationServer @EnableWebSecurity public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
@Configuration @ImportResource("classpath:/client.xml") public class OauthConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.tokenServices(tokenServices(endpoints)).authenticationManager(authenticationManager); } private DefaultTokenServices tokenServices(AuthorizationServerEndpointsConfigurer endpoints) { DefaultTokenServices services = new DefaultTokenServices(); services.setTokenStore(tokenStore()); services.setSupportRefreshToken(true); services.setReuseRefreshToken(false); services.setClientDetailsService(endpoints.getClientDetailsService()); return services; } private TokenStore tokenStore() { return new InMemoryTokenStore(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oauth2="http://www.springframework.org/schema/security/oauth2" xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <oauth2:client-details-service id="clientDetailsService"> <oauth2:client client-id="client1" secret="secret1" authorized-grant-types="password,refresh_token" access-token-validity="1800" refresh-token-validity="604800" scope="all" /> </oauth2:client-details-service> </beans>
@Component public class CustomUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { return new User("user", "pwd", AuthorityUtils.createAuthorityList("ROLE_USER")); } }
測試方法框架
{ "access_token": "352d9a1c-86aa-4011-9732-4beca4d9f848", "token_type": "bearer", "refresh_token": "c2295cbf-e33c-4fac-a4c8-eaea25c4c72b", "expires_in": 1799, "scope": "all" }
至此便構建了一個簡單版的OAuth服務器maven
後面在 使用Spring Boot構建獨立的OAuth服務器(二) 中會進行更多的配置。