最近公司系统重构,须要提供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服务器(二) 中会进行更多的配置。