OAuth2.0-3客户端授权放到数据库
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了OAuth2.0-3客户端授权放到数据库,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6161字,纯文字阅读大概需要9分钟。
内容图文
![OAuth2.0-3客户端授权放到数据库](/upload/InfoBanner/zyjiaocheng/524/7cb9c87a029848e6aa6f02e4524ee3f0.jpg)
//造点测试数据
/*
INSERT INTO `oauth_client_details` VALUES (‘client1‘, ‘resource1‘, ‘$2a$10$YEpRG0cFXz5yfC/lKoCHJ.83r/K3vaXLas5zCeLc.EJsQ/gL5Jvum‘, ‘scope1,scope2‘, ‘authorization_code,password,client_credentials,implicit,refresh_token‘, ‘http://www.baidu.com‘, null, ‘300‘, ‘1500‘, null, ‘false‘);*/
2.配置数据源 yml里配置后注入到clientDetailService
server: port: 8001 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root url: mysql://192.168.3.158:3306/test?&useUnicode=true&characterEncoding=utf-8
3.配置clientDetailService
@Autowired private DataSource dataSource; //配置客户端 @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { //配置客户端存储到db 代替原来得内存模式 JdbcClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource); clientDetailsService.setPasswordEncoder(passwordEncoder); clients.withClientDetails(clientDetailsService); }
4.授权码存到数据库
@Configuration public class TokenConfig { @Autowired private DataSource dataSource; //配置token的存储方法 @Bean public TokenStore tokenStore() { //配置token存储在数据库 return new JdbcTokenStore(dataSource); } }
//配置token管理服务 @Bean public AuthorizationServerTokenServices tokenServices() { DefaultTokenServices defaultTokenServices = new DefaultTokenServices(); defaultTokenServices.setClientDetailsService(clientDetailsService); defaultTokenServices.setSupportRefreshToken(true); //配置token的存储方法 defaultTokenServices.setTokenStore(tokenStore); defaultTokenServices.setAccessTokenValiditySeconds(300); defaultTokenServices.setRefreshTokenValiditySeconds(1500); return defaultTokenServices; }
以上完整代码:
AuthorizationServerConfig
@Configuration //开启oauth2,auth server模式 @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private PasswordEncoder passwordEncoder; @Autowired private DataSource dataSource; //配置客户端 @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { //配置客户端存储到db JdbcClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource); clientDetailsService.setPasswordEncoder(passwordEncoder); clients.withClientDetails(clientDetailsService); } @Autowired private ClientDetailsService clientDetailsService; @Autowired private TokenStore tokenStore; //配置token管理服务 @Bean public AuthorizationServerTokenServices tokenServices() { DefaultTokenServices defaultTokenServices = new DefaultTokenServices(); defaultTokenServices.setClientDetailsService(clientDetailsService); defaultTokenServices.setSupportRefreshToken(true); //配置token的存储方法 defaultTokenServices.setTokenStore(tokenStore); defaultTokenServices.setAccessTokenValiditySeconds(300); defaultTokenServices.setRefreshTokenValiditySeconds(1500); return defaultTokenServices; } //密码模式才需要配置,认证管理器 @Autowired private AuthenticationManager authenticationManager; //把上面的各个组件组合在一起 @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager)//认证管理器 //配置授权码存储到db .authorizationCodeServices(new JdbcAuthorizationCodeServices(dataSource))//授权码管理 .tokenServices(tokenServices())//token管理 .allowedTokenEndpointRequestMethods(HttpMethod.POST); } //配置哪些接口可以被访问 @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security.tokenKeyAccess("permitAll()")///oauth/token_key公开 .checkTokenAccess("permitAll()")///oauth/check_token公开 .allowFormAuthenticationForClients();//允许表单认证 } }
TokenConfig
@Configuration public class TokenConfig { @Autowired private DataSource dataSource; //配置token的存储方法 @Bean public TokenStore tokenStore() { //配置token存储在内存中,这种是普通token,每次都需要远程校验,性能较差 return new JdbcTokenStore(dataSource); } }
WebSecurityConfig
@Configuration @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } //密码模式才需要配置,认证管理器 @Bean @Override protected AuthenticationManager authenticationManager() throws Exception { return super.authenticationManager(); } @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() .anyRequest().permitAll() .and() .formLogin() .and() .logout(); } @Bean public UserDetailsService userDetailsService() { return s -> { if ("admin".equals(s) || "user".equals(s)) { return new MyUserDetails(s, passwordEncoder().encode(s), s); } return null; }; } }
验证:
//授权码模式
//浏览器访问
http://127.0.0.1:8001/oauth/authorize?client_id=client1&response_type=code&scope=scope1&redirect_uri=http://www.baidu.com
授权码也存入了数据库:
postman申请令牌
已经存入了数据库
验证令牌
OAuth2.0-3客户端授权放到数据库
标签:access 信息 auth ORC div ast master 组合 class
本文系统来源:https://www.cnblogs.com/jiawen010/p/12979751.html
内容总结
以上是互联网集市为您收集整理的OAuth2.0-3客户端授权放到数据库全部内容,希望文章能够帮你解决OAuth2.0-3客户端授权放到数据库所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。