博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Spring Boot构建独立的OAuth服务器(一)
阅读量:6955 次
发布时间:2019-06-27

本文共 3405 字,大约阅读时间需要 11 分钟。

hot3.png

最近公司系统重构,需要提供API接口给其他部门调用,由于架构原因,这些API有可能会被外部访问,基于安全性的考虑,决定使用OAuth来保护这些API,以免被随意调用。

由于系统众多,不可能在每个系统中都配置OAuth认证授权功能,因此需要构建一个独立的OAuth服务器,专门负责认证授权,这里采用的框架是Spring Boot。

整个认证授权流程中有三个角色:

  • 客户端(Client)
  • API接口(Resource)
  • OAuth服务器(Authorization Server)

授权模式有四种:

  • 授权码模式(Authorization Code)
  • 简化模式(Implicit)
  • 密码模式(Resource Owner Password Credentials)
  • 客户端模式(Client Credentials)

具体定义可看理解 

因为访问OAuth服务器的都是公司内部系统,并且不可能使用同一个登录页面,所以只有密码模式适用,因此后面配置的时候也只配置密码模式。

具体流程如下图

  1. 客户端向OAuth服务器申请Access Token
  2. 认证授权成功后OAuth服务器会返回Access Token给客户端
  3. 客户端带着Access Token调用API接口
  4. API接口把Access Token交给OAuth服务器检查
  5. 如果Access Token有效,OAuth服务器会返回用户相关信息(用户名,角色等)给API接口
  6. API接口根据检查结果来决定返回给客户端的内容

下面开始实现一个简单版的OAuth服务器

  1. 在pom.xml中配置依赖包和插件
    org.springframework.boot
    spring-boot-starter-parent
    1.5.6.RELEASE
    org.springframework.boot
    spring-boot-starter-web
    org.springframework.security.oauth
    spring-security-oauth2
    org.springframework.boot
    spring-boot-maven-plugin

     

  2. 编写主类Application
    @SpringBootApplication@EnableAuthorizationServer@EnableWebSecuritypublic class Application {	public static void main(String[] args) {		SpringApplication.run(Application.class, args);	}}

     

  3. 编写OAuth配置类OauthConfig,这里将Token存储在内存中
    @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();	}}

     

  4. 在client.xml中配置client信息,这里使用xml文件来配置是因为觉得管理方便

     

  5. 编写用户查询类CustomUserDetailsService,定义一个固定用户,用户名为user,密码为pwd,角色为ROLE_USER
    @Componentpublic class CustomUserDetailsService implements UserDetailsService {	@Override	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {		return new User("user", "pwd", AuthorityUtils.createAuthorityList("ROLE_USER"));	}}

     

测试方法

  1. 使用Maven运行Goal:clean spring-boot:run
  2. 使用接口调用工具,如Postman,配置HTTP Basic认证,用户名和密码分别对应client.xml中的client-id和secret,使用POST方法和参数grant_type:password,username:user,password:pwd来调用http://localhost:8080/oauth/token,若调用成功便会返回如下格式的JSON字符串
    {    "access_token": "352d9a1c-86aa-4011-9732-4beca4d9f848",    "token_type": "bearer",    "refresh_token": "c2295cbf-e33c-4fac-a4c8-eaea25c4c72b",    "expires_in": 1799,    "scope": "all"}

     

至此便构建了一个简单版的OAuth服务器

后面在  中会进行更多的配置。

转载于:https://my.oschina.net/u/3707083/blog/1550787

你可能感兴趣的文章
Java核心技术卷一基础知识-第5章-继承-读书笔记
查看>>
解决了64位 window 8英文版 office 2013 word繁简转换的问题( 看图)
查看>>
【机器学习】--贝叶斯网络
查看>>
C# 中使用 Task 实现提前加载
查看>>
php---依赖倒转(反转控制)原则
查看>>
团队编程项目开发环境搭建过程
查看>>
iOS 页面与页面之间传参数的方法 代码块传值
查看>>
POJ 2503
查看>>
基于 HTML5 结合互联网+的电力接线图
查看>>
range与xrange
查看>>
Kali渗透测试——HexInject
查看>>
中国式人机协作
查看>>
进程 子进程 关系
查看>>
孟岩:通证(token)和通证经济的目的在于改善现有经济的效率性
查看>>
杜鹃演绎奢华春装大片
查看>>
mongoDb
查看>>
windows cmd 命令和 linux 命令
查看>>
vs extension
查看>>
代码度量工具——SourceMonitor的学习和使用
查看>>
201521123081《java程序设计》 第11周学习总结
查看>>