package com.plat.config;
import com.plat.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.approval.ApprovalStore;
import org.springframework.security.oauth2.provider.approval.JdbcApprovalStore;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
@EnableAuthorizationServer //OAuth2的授权服务器
public class OAuth2ServiceConfig implements AuthorizationServerConfigurer {
@Resource(name = "jdbcTokenStore")
private TokenStore tokenStore; //注入自定义的token存储配置Bean
@Resource(name = "jdbcClientDetailsService")
private ClientDetailsService clientDetailsService; //客户端角色详情
@Resource
private AuthenticationManager authenticationManager; //注入安全配置类中定义的认证管理器Bean
@Resource(name = "jdbcAuthorizationCodeServices")
private AuthorizationCodeServices authorizationCodeServices; //注入自定义的授权码模式服务配置Bean
@Resource(name = "jdbcApprovalStore")
private ApprovalStore approvalStore; //授权信息保存策略
//token令牌管理
@Bean
public AuthorizationServerTokenServices tokenServices() {
DefaultTokenServices tokenServices = new DefaultTokenServices();
tokenServices.setClientDetailsService(clientDetailsService); //客户端信息服务,即向哪个客户端颁发令牌
tokenServices.setSupportRefreshToken(true); //支持产生刷新令牌
tokenServices.setTokenStore(tokenStore); //令牌的存储策略
tokenServices.setAccessTokenValiditySeconds(7200); //令牌默认有效期2小时
tokenServices.setRefreshTokenValiditySeconds(259200); //refresh_token默认有效期三天
return tokenServices;
}
/**
* token令牌端点访问的安全策略
* (不是所有人都可以来访问框架提供的这些令牌端点的)
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer authorizationServerSecurityConfigurer) throws Exception {
authorizationServerSecurityConfigurer.tokenKeyAccess("permitAll()") //oauth/token_key这个端点(url)是公开的,不用登录可调
.checkTokenAccess("permitAll()") // oauth/check_token这个端点是公开的
.allowFormAuthenticationForClients(); //允许客户端表单认证,申请令牌
}
/**
* Oauth2.0客户端角色的信息来源:内存、数据库
* 这里用数据库
*/
@Override
public void configure(ClientDetailsServiceConfigurer clientDetailsServiceConfigurer) throws Exception {
clientDetailsServiceConfigurer.withClientDetails(clientDetailsService);
}
/**
* 令牌端点访问和令牌服务(令牌怎么生成、怎么存储等)
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager) //设置认证管理器,密码模式需要
.authorizationCodeServices(authorizationCodeServices) //授权码模式需要
.approvalStore(approvalStore)
.tokenServices(tokenServices()) //token管理服务
.allowedTokenEndpointRequestMethods(HttpMethod.POST); //允许Post方式访问
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
OAuth2授权服务+资源服务Demo源码
共50个文件
java:17个
class:17个
yml:4个
0 下载量 89 浏览量
2024-03-21
19:46:31
上传
评论
收藏 45KB ZIP 举报
温馨提示
授权服务+资源服务Demo源码
资源推荐
资源详情
资源评论
收起资源包目录
OAuth.zip (50个子文件)
resource-svc
HELP.md 853B
pom.xml 4KB
src
test
java
com
plat
ResourceSvcApplicationTests.java 214B
main
resources
static
index.html 79B
application.yml 454B
java
com
plat
ResourceSvcApplication.java 668B
controller
ResourceController.java 407B
config
BeanConfig.java 711B
OAuthSourceConfig.java 2KB
resource-svc.iml 11KB
target
classes
static
index.html 79B
application.yml 455B
com
plat
ResourceSvcApplication.class 1KB
controller
ResourceController.class 734B
config
OAuthSourceConfig.class 5KB
BeanConfig.class 1012B
test-classes
com
plat
ResourceSvcApplicationTests.class 536B
.gitignore 395B
auth
HELP.md 853B
auth.iml 11KB
pom.xml 4KB
src
test
java
com
plat
AuthApplicationTests.java 207B
main
resources
static
index.html 79B
application.yml 454B
java
com
plat
mapper
UserMapper.java 476B
RoleMapper.java 629B
service
UserService.java 171B
impl
UserServiceImpl.java 1KB
AuthApplication.java 340B
domain
po
SysUserPo.java 515B
SysRolePo.java 485B
vo
SecurityUser.java 1KB
config
WebSecurityConfig.java 2KB
OAuth2Bean.java 2KB
OAuth2ServiceConfig.java 5KB
target
classes
static
index.html 79B
application.yml 455B
com
plat
mapper
RoleMapper.class 779B
UserMapper.class 628B
AuthApplication.class 725B
service
UserService.class 192B
impl
UserServiceImpl.class 2KB
domain
po
SysUserPo.class 822B
SysRolePo.class 693B
vo
SecurityUser.class 1KB
config
WebSecurityConfig.class 3KB
OAuth2ServiceConfig.class 5KB
OAuth2Bean.class 2KB
test-classes
com
plat
AuthApplicationTests.class 515B
.gitignore 395B
共 50 条
- 1
资源评论
-代号9527
- 粉丝: 1w+
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功