/*
* Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
*
* https://www.gz-yami.com/
*
* 未经允许,不可做商业用途!
*
* 版权所有,侵权必究!
*/
package com.yami.shop.security.util;
import java.util.Date;
import java.util.Set;
import java.util.UUID;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken;
import org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.InvalidScopeException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
import org.springframework.security.oauth2.provider.TokenRequest;
import org.springframework.security.oauth2.provider.token.*;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
/**
* Base implementation for token services using random UUID values for the access token and refresh token values. The
* main extension point for customizations is the {@link TokenEnhancer} which will be called after the access and
* refresh tokens have been generated but before they are stored.
* <p>
* Persistence is delegated to a {@code TokenStore} implementation and customization of the access token to a
* {@link TokenEnhancer}.
*
* @author Ryan Heaton
* @author Luke Taylor
* @author Dave Syer
* @author LGH
*/
public class YamiTokenServices implements AuthorizationServerTokenServices, ResourceServerTokenServices,
ConsumerTokenServices, InitializingBean {
// default 30 days.
private int refreshTokenValiditySeconds = 60 * 60 * 24 * 30;
// default 12 hours.
private int accessTokenValiditySeconds = 60 * 60 * 12;
private boolean supportRefreshToken = false;
private boolean reuseRefreshToken = true;
private TokenStore tokenStore;
private ClientDetailsService clientDetailsService;
private TokenEnhancer accessTokenEnhancer;
private AuthenticationManager authenticationManager;
/**
* Initialize these token services. If no random generator is set, one will be created.
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(tokenStore, "tokenStore must be set");
}
@Transactional(rollbackFor = Exception.class)
@Override
public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) {
OAuth2AccessToken existingAccessToken = tokenStore.getAccessToken(authentication);
OAuth2RefreshToken refreshToken = null;
// 如果有token,直接删除,更新token,避免出现缓存问题
// if (existingAccessToken != null) {
// if (existingAccessToken.getRefreshToken() != null) {
// refreshToken = existingAccessToken.getRefreshToken();
// // The token store could remove the refresh token when the
// // access token is removed, but we want to
// // be sure...
// tokenStore.removeRefreshToken(refreshToken);
// }
// tokenStore.removeAccessToken(existingAccessToken);
//
// }
// Only create a new refresh token if there wasn't an existing one
// associated with an expired access token.
// Clients might be holding existing refresh tokens, so we re-use it in
// the case that the old access token
// expired.
if (refreshToken == null) {
refreshToken = createRefreshToken(authentication);
}
// But the refresh token itself might need to be re-issued if it has
// expired.
else if (refreshToken instanceof ExpiringOAuth2RefreshToken) {
ExpiringOAuth2RefreshToken expiring = (ExpiringOAuth2RefreshToken) refreshToken;
if (System.currentTimeMillis() > expiring.getExpiration().getTime()) {
refreshToken = createRefreshToken(authentication);
}
}
OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken);
tokenStore.storeAccessToken(accessToken, authentication);
// In case it was modified
refreshToken = accessToken.getRefreshToken();
if (refreshToken != null) {
tokenStore.storeRefreshToken(refreshToken, authentication);
}
return accessToken;
}
@Override
@Transactional(noRollbackFor={InvalidTokenException.class, InvalidGrantException.class}, rollbackFor = Exception.class)
public OAuth2AccessToken refreshAccessToken(String refreshTokenValue, TokenRequest tokenRequest)
throws AuthenticationException {
if (!supportRefreshToken) {
throw new InvalidGrantException("Invalid refresh token: " + refreshTokenValue);
}
OAuth2RefreshToken refreshToken = tokenStore.readRefreshToken(refreshTokenValue);
if (refreshToken == null) {
throw new InvalidGrantException("Invalid refresh token: " + refreshTokenValue);
}
OAuth2Authentication authentication = tokenStore.readAuthenticationForRefreshToken(refreshToken);
if (this.authenticationManager != null && !authentication.isClientOnly()) {
// The client has already been authenticated, but the user authentication might be old now, so give it a
// chance to re-authenticate.
Authentication user = new PreAuthenticatedAuthenticationToken(authentication.getUserAuthentication(), "", authentication.getAuthorities());
user = authenticationManager.authenticate(user);
Object details = authentication.getDetails();
authentication = new OAuth2Authentication(authentication.getOAuth2Request(), user);
authentication.setDetails(details);
}
String clientId = authentication.getOAuth2Request().getClientId();
if (clientId == null || !clientId.equals(tokenRequest.getClientId())) {
throw new InvalidGrantException("Wrong client for this refresh token: " + refreshTokenValue);
}
// clear out any access tokens already associated with the refresh
// token.
tokenStore.removeAccessTokenUsingRefreshToken(refreshToken);
if (isExpired(refreshToken)) {
tokenStore.removeRefreshToken(refreshToken);
throw new InvalidTokenException("Invalid refresh token (expired): " + refreshToken);
}
authentication = createRefreshedAuthentication(authentication, tokenRequest);
if (!reuseRefreshToken) {
tokenStore.removeRefreshToken(refreshToken);
refreshToken = createRefreshToken(authentication);
}
OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken);
tokenStore.storeAccessToken(accessToken, authentication);
if (!reuseRefreshToken) {
没有合适的资源?快使用搜索试试~ 我知道了~
分销系统源码Java.zip
共2000个文件
java:866个
js:508个
png:378个
需积分: 0 55 下载量 174 浏览量
2020-08-28
18:07:39
上传
评论 7
收藏 34.32MB ZIP 举报
温馨提示
分销系统,明细设计文档都在。前后端完整版系统。部署上线在用中。枕套分享出来共而开或者毕设学习使用。本地部署以及线上部署方案都在里面包含。为什么分享出来呢。你就当我无私奉献吧
资源推荐
资源详情
资源评论
收起资源包目录
分销系统源码Java.zip (2000个子文件)
style.css 212KB
style.css 212KB
animate.css 73KB
animate.css 73KB
guildford.css 58KB
guildford.css 58KB
view.css 49KB
skin.min.css 43KB
skin.min.css 43KB
iconfont.css 32KB
iconfont.css 32KB
style.css 31KB
style.css 31KB
skin.mobile.min.css 27KB
skin.mobile.min.css 27KB
visualblocks.css 5KB
visualblocks.css 5KB
parse.css 4KB
content.min.css 4KB
content.min.css 4KB
content.inline.min.css 3KB
content.inline.min.css 3KB
prism.css 2KB
prism.css 2KB
content.mobile.min.css 234B
content.mobile.min.css 234B
__uniappview.html 982B
index.html 929B
index.html 929B
YamiTokenServices.java 18KB
YamiTokenServices.java 18KB
OrderController.java 12KB
OrderController.java 12KB
ShopCartController.java 11KB
ShopCartController.java 11KB
SubmitOrderListener.java 11KB
SubmitOrderListener.java 11KB
MyOrderController.java 9KB
MyOrderController.java 9KB
ProductController.java 8KB
ProductController.java 8KB
BasketServiceImpl.java 8KB
BasketServiceImpl.java 8KB
SmsLogServiceImpl.java 8KB
SmsLogServiceImpl.java 8KB
OrderController.java 8KB
OrderController.java 8KB
ProductServiceImpl.java 7KB
ProductServiceImpl.java 7KB
AddrController.java 7KB
AddrController.java 7KB
OrderServiceImpl.java 7KB
OrderServiceImpl.java 7KB
SysUserController.java 6KB
SysUserController.java 6KB
TransportServiceImpl.java 6KB
TransportServiceImpl.java 6KB
LoginAuthenticationFilter.java 6KB
LoginAuthenticationFilter.java 6KB
TransportManagerServiceImpl.java 6KB
TransportManagerServiceImpl.java 6KB
LoginAuthenticationFilter.java 6KB
LoginAuthenticationFilter.java 6KB
SysMenuController.java 6KB
SysMenuController.java 6KB
ScheduleManager.java 5KB
ScheduleManager.java 5KB
ProdController.java 5KB
ProdController.java 5KB
ShopDetailController.java 5KB
ShopDetailController.java 5KB
CategoryServiceImpl.java 5KB
CategoryServiceImpl.java 5KB
Json.java 5KB
Json.java 5KB
CategoryController.java 5KB
CategoryController.java 5KB
ScheduleJobController.java 5KB
ScheduleJobController.java 5KB
ShopDetailParam.java 5KB
ShopDetailParam.java 5KB
UserCollectionController.java 4KB
UserCollectionController.java 4KB
SpecController.java 4KB
SpecController.java 4KB
PayServiceImpl.java 4KB
PayServiceImpl.java 4KB
ProdTagController.java 4KB
ProdTagController.java 4KB
WxPortalController.java 4KB
WxPortalController.java 4KB
WxMaInRedisConfig.java 4KB
WxMaInRedisConfig.java 4KB
RedisUtil.java 4KB
RedisUtil.java 4KB
TransportController.java 4KB
TransportController.java 4KB
YamiUserServiceImpl.java 4KB
YamiUserServiceImpl.java 4KB
MessageController.java 4KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
道1993
- 粉丝: 9770
- 资源: 44
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功