系统采用前后端分离的架构,采用OAuth2协议是很自然的事情。

下面开始实战,主要依赖以下两个组件:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
例外还要配置两个Config:

一、认证服务器

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter { @Autowired
 private UserApprovalHandler userApprovalHandler; @Autowired
 private AuthenticationManager authenticationManager; @Autowired
 private TokenStore tokenStore; @Autowired
 private MyUserService userService; @Autowired
 private ClientDetailsService clientDetailsService; @Override
 public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("aizoukeji")
// .authorizedGrantTypes("password", "authorization_code", "implicit")
 .authorizedGrantTypes("password")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("read", "write", "trust")
.secret("18657189775")
.accessTokenValiditySeconds(60 * 2);//Access token is only valid for 2 minutes.
// refreshTokenValiditySeconds(600);//Refresh token is only valid for 10 minutes.
 } @Override
 public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore)
.userApprovalHandler(userApprovalHandler)
.authenticationManager(authenticationManager)
.userDetailsService(userService);
} @Bean
 public TokenStore tokenStore() {
return new InMemoryTokenStore();
} @Bean
@Autowired
 public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
handler.setTokenStore(tokenStore);
handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
handler.setClientDetailsService(clientDetailsService);
return handler;
} @Bean
@Autowired
 public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
TokenApprovalStore store = new TokenApprovalStore();
store.setTokenStore(tokenStore);
return store;
}
}

二、资源服务器

@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
private static final String RESOURCE_ID = "my_rest_api"; @Override
 public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId(RESOURCE_ID).stateless(true);
} @Override
 public void configure(HttpSecurity http) throws Exception {
// http.requestMatchers().antMatchers("/**")
// .and()
// .authorizeRequests().antMatchers("/v1/**").authenticated()
// .and()
// .exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());  http.authorizeRequests().antMatchers("/v1/**").authenticated()
.and()
.exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
}
}

踩过的坑

一开始一直在配置WebSecurityConfigurerAdapter,其实这个跟ResourceServerConfigurerAdapter是冲突的,如果用OAuth来做认证的话,那么只要配置ResourceServerConfigurerAdapter就可以了

延伸

Spring OAuth中有个SSO注解,可以帮助实现单点登录。等项目发展起来以后,我们可以用这个来实现账号的统一授权。

最新文章

  1. PHP include 和 require 语句
  2. Unity响应Android的返回键,退出当前Activity
  3. Make it run, make it right, make it fast
  4. Javascript history pushState onpopstate方法做AJAX SEO
  5. HTML练习----注册界面
  6. License Manager 10.3启动失败解决方法
  7. HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效。
  8. JavaScript高级程序设计25.pdf
  9. 我为什么要再给lua写一个json模块
  10. WCF扩展之实现ZeroMQ绑定和protocolBuffer消息编码(二)实现IRequestChannel(2016-03-15 12:35)
  11. Spring Tool Suite生成默认的MVC项目的配置文件问题
  12. 让intellij idea 14 支持ES6语法
  13. MongoDB 运维相关的命令
  14. 20165231 2017-2018-2 《Java程序设计》第9周学习总结
  15. 安全工具-cansina
  16. android http中请求访问添加 cookie
  17. 201621123001 《Java程序设计》第9周学习总结
  18. vue 插件tab选项卡(转载)
  19. 深入浅出Mybatis技术原理与实战(杨开振)(带详细书签) PDF 下载 高清 完整版+源码
  20. python框架----&gt;BeautifulSoup的使用

热门文章

  1. 字母加密-C基础
  2. JSON工具类的构建(后端版本)
  3. jQuery部分疑问及小结
  4. android中的国际化
  5. swagger2.0与spring结合
  6. 论文学习——《Learning to Compose with Professional Photographs on the Web》 (ACM MM 2017)
  7. 1、cmd中检测远程的ip和端口是否处于监听状态
  8. ORM与JPA规范
  9. Fabric.js的使用
  10. PowerPoint for Mac 中的键盘快捷方式汇总