mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-14 06:09:03 +08:00
🎨 改进结构和代码格式. add spring-javaformat plugin and format all code
This commit is contained in:
1 parent
5414983f3d
commit
bd47066299
651 files changed
+13034
-12462
No files matched your search
@@ -19,15 +19,13 @@
|
||||
|
||||
package com.pig4cloud.pigx.auth;
|
||||
|
||||
|
||||
import com.pig4cloud.pigx.common.feign.annotation.EnablePigxFeignClients;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.cloud.client.SpringCloudApplication;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020-02-08
|
||||
* 认证授权中心
|
||||
* @date 2020-02-08 认证授权中心
|
||||
*/
|
||||
@SpringCloudApplication
|
||||
@EnablePigxFeignClients
|
||||
@@ -36,4 +34,5 @@ public class PigxAuthApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PigxAuthApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+11
-14
@@ -45,17 +45,21 @@ import org.springframework.security.oauth2.provider.token.store.redis.RedisToken
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/6/22
|
||||
* 认证服务器配置
|
||||
* @date 2018/6/22 认证服务器配置
|
||||
*/
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
@EnableAuthorizationServer
|
||||
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
|
||||
|
||||
private final ClientDetailsService pigxClientDetailsServiceImpl;
|
||||
|
||||
private final AuthenticationManager authenticationManagerBean;
|
||||
|
||||
private final RedisConnectionFactory redisConnectionFactory;
|
||||
|
||||
private final UserDetailsService pigxUserDetailsService;
|
||||
|
||||
private final TokenEnhancer pigxTokenEnhancer;
|
||||
|
||||
@Override
|
||||
@@ -66,25 +70,18 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
|
||||
|
||||
@Override
|
||||
public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
|
||||
oauthServer
|
||||
.allowFormAuthenticationForClients()
|
||||
.checkTokenAccess("isAuthenticated()");
|
||||
oauthServer.allowFormAuthenticationForClients().checkTokenAccess("isAuthenticated()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
|
||||
endpoints
|
||||
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
|
||||
.tokenStore(tokenStore())
|
||||
.tokenEnhancer(pigxTokenEnhancer)
|
||||
.userDetailsService(pigxUserDetailsService)
|
||||
.authenticationManager(authenticationManagerBean)
|
||||
.reuseRefreshTokens(false)
|
||||
endpoints.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST).tokenStore(tokenStore())
|
||||
.tokenEnhancer(pigxTokenEnhancer).userDetailsService(pigxUserDetailsService)
|
||||
.authenticationManager(authenticationManagerBean).reuseRefreshTokens(false)
|
||||
.pathMapping("/oauth/confirm_access", "/token/confirm_access")
|
||||
.exceptionTranslator(new PigxWebResponseExceptionTranslator());
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public TokenStore tokenStore() {
|
||||
RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
|
||||
@@ -97,5 +94,5 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
|
||||
});
|
||||
return tokenStore;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -39,43 +39,28 @@ import org.springframework.security.web.authentication.AuthenticationSuccessHand
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/6/22
|
||||
* 认证相关配置
|
||||
* @date 2018/6/22 认证相关配置
|
||||
*/
|
||||
@Primary
|
||||
@Order(90)
|
||||
@Configuration
|
||||
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
protected void configure(HttpSecurity http) {
|
||||
http
|
||||
.formLogin()
|
||||
.loginPage("/token/login")
|
||||
.loginProcessingUrl("/token/form")
|
||||
.failureHandler(authenticationFailureHandler())
|
||||
.and()
|
||||
.logout()
|
||||
http.formLogin().loginPage("/token/login").loginProcessingUrl("/token/form")
|
||||
.failureHandler(authenticationFailureHandler()).and().logout()
|
||||
.logoutSuccessHandler((request, response, authentication) -> {
|
||||
String referer = request.getHeader(HttpHeaders.REFERER);
|
||||
response.sendRedirect(referer);
|
||||
})
|
||||
.deleteCookies("JSESSIONID")
|
||||
.invalidateHttpSession(true)
|
||||
.and()
|
||||
.authorizeRequests()
|
||||
.antMatchers(
|
||||
"/token/**",
|
||||
"/actuator/**",
|
||||
"/mobile/**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
.and().csrf().disable()
|
||||
.apply(mobileSecurityConfigurer());
|
||||
}).deleteCookies("JSESSIONID").invalidateHttpSession(true).and().authorizeRequests()
|
||||
.antMatchers("/token/**", "/actuator/**", "/mobile/**").permitAll().anyRequest().authenticated().and()
|
||||
.csrf().disable().apply(mobileSecurityConfigurer());
|
||||
}
|
||||
|
||||
/**
|
||||
* 不拦截静态资源
|
||||
*
|
||||
* @param web
|
||||
*/
|
||||
@Override
|
||||
@@ -105,11 +90,9 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
|
||||
return new MobileSecurityConfigurer();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-updated
|
||||
* Encoded password does not look like BCrypt
|
||||
*
|
||||
* @return PasswordEncoder
|
||||
*/
|
||||
@Bean
|
||||
|
||||
@@ -59,25 +59,29 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/6/24
|
||||
* 删除token端点
|
||||
* @date 2018/6/24 删除token端点
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/token")
|
||||
public class PigxTokenEndpoint {
|
||||
private static final String PIGX_OAUTH_ACCESS = SecurityConstants.PIGX_PREFIX + SecurityConstants.OAUTH_PREFIX + "auth_to_access:";
|
||||
|
||||
private static final String PIGX_OAUTH_ACCESS = SecurityConstants.PIGX_PREFIX + SecurityConstants.OAUTH_PREFIX
|
||||
+ "auth_to_access:";
|
||||
|
||||
private final ClientDetailsService clientDetailsService;
|
||||
|
||||
private final RedisTemplate redisTemplate;
|
||||
|
||||
private final TokenStore tokenStore;
|
||||
|
||||
private final CacheManager cacheManager;
|
||||
|
||||
/**
|
||||
* 认证页面
|
||||
*
|
||||
* @param modelAndView
|
||||
* @param error 表单登录失败处理回调的错误信息
|
||||
* @param error 表单登录失败处理回调的错误信息
|
||||
* @return ModelAndView
|
||||
*/
|
||||
@GetMapping("/login")
|
||||
@@ -89,7 +93,6 @@ public class PigxTokenEndpoint {
|
||||
|
||||
/**
|
||||
* 确认授权页面
|
||||
*
|
||||
* @param request
|
||||
* @param session
|
||||
* @param modelAndView
|
||||
@@ -114,7 +117,6 @@ public class PigxTokenEndpoint {
|
||||
|
||||
/**
|
||||
* 退出token
|
||||
*
|
||||
* @param authHeader Authorization
|
||||
*/
|
||||
@DeleteMapping("/logout")
|
||||
@@ -129,7 +131,6 @@ public class PigxTokenEndpoint {
|
||||
|
||||
/**
|
||||
* 令牌管理调用
|
||||
*
|
||||
* @param token token
|
||||
* @return
|
||||
*/
|
||||
@@ -143,8 +144,7 @@ public class PigxTokenEndpoint {
|
||||
|
||||
OAuth2Authentication auth2Authentication = tokenStore.readAuthentication(accessToken);
|
||||
// 清空用户信息
|
||||
cacheManager.getCache(CacheConstants.USER_DETAILS)
|
||||
.evict(auth2Authentication.getName());
|
||||
cacheManager.getCache(CacheConstants.USER_DETAILS).evict(auth2Authentication.getName());
|
||||
|
||||
// 清空access token
|
||||
tokenStore.removeAccessToken(accessToken);
|
||||
@@ -155,35 +155,33 @@ public class PigxTokenEndpoint {
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询token
|
||||
*
|
||||
* @param params 分页参数
|
||||
* @return
|
||||
*/
|
||||
@Inner
|
||||
@PostMapping("/page")
|
||||
public R<Page> tokenList(@RequestBody Map<String, Object> params) {
|
||||
//根据分页参数获取对应数据
|
||||
// 根据分页参数获取对应数据
|
||||
String key = String.format("%s*:%s", PIGX_OAUTH_ACCESS, TenantContextHolder.getTenantId());
|
||||
List<String> pages = findKeysForPage(key, MapUtil.getInt(params, PaginationConstants.CURRENT)
|
||||
, MapUtil.getInt(params, PaginationConstants.SIZE));
|
||||
List<String> pages = findKeysForPage(key, MapUtil.getInt(params, PaginationConstants.CURRENT),
|
||||
MapUtil.getInt(params, PaginationConstants.SIZE));
|
||||
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
|
||||
Page result = new Page(MapUtil.getInt(params, PaginationConstants.CURRENT), MapUtil.getInt(params, PaginationConstants.SIZE));
|
||||
Page result = new Page(MapUtil.getInt(params, PaginationConstants.CURRENT),
|
||||
MapUtil.getInt(params, PaginationConstants.SIZE));
|
||||
result.setRecords(redisTemplate.opsForValue().multiGet(pages));
|
||||
result.setTotal(redisTemplate.keys(key).size());
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
|
||||
private List<String> findKeysForPage(String patternKey, int pageNum, int pageSize) {
|
||||
ScanOptions options = ScanOptions.scanOptions().count(1000L)
|
||||
.match(patternKey).build();
|
||||
ScanOptions options = ScanOptions.scanOptions().count(1000L).match(patternKey).build();
|
||||
RedisSerializer<String> redisSerializer = (RedisSerializer<String>) redisTemplate.getKeySerializer();
|
||||
Cursor cursor = (Cursor) redisTemplate.executeWithStickyConnection(redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize));
|
||||
Cursor cursor = (Cursor) redisTemplate.executeWithStickyConnection(
|
||||
redisConnection -> new ConvertingCursor<>(redisConnection.scan(options), redisSerializer::deserialize));
|
||||
List<String> result = new ArrayList<>();
|
||||
int tmpIndex = 0;
|
||||
int startIndex = (pageNum - 1) * pageSize;
|
||||
@@ -205,9 +203,11 @@ public class PigxTokenEndpoint {
|
||||
|
||||
try {
|
||||
cursor.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (IOException e) {
|
||||
log.error("关闭cursor 失败");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
+7
-5
@@ -44,21 +44,22 @@ import javax.servlet.http.HttpServletResponse;
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class PigxAuthenticationFailureEventHandler implements AuthenticationFailureHandler {
|
||||
|
||||
private final RemoteLogService logService;
|
||||
|
||||
/**
|
||||
* 异步处理,登录失败方法
|
||||
* <p>
|
||||
*
|
||||
* @param authenticationException 登录的authentication 对象
|
||||
* @param authentication 登录的authenticationException 对象
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
* @param authentication 登录的authenticationException 对象
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
*/
|
||||
@Async
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void handle(AuthenticationException authenticationException, Authentication authentication, HttpServletRequest request, HttpServletResponse response) {
|
||||
public void handle(AuthenticationException authenticationException, Authentication authentication,
|
||||
HttpServletRequest request, HttpServletResponse response) {
|
||||
String username = authentication.getName();
|
||||
SysLog sysLog = SysLogUtils.getSysLog(request, username);
|
||||
sysLog.setTitle(username + "用户登录");
|
||||
@@ -72,4 +73,5 @@ public class PigxAuthenticationFailureEventHandler implements AuthenticationFail
|
||||
|
||||
log.info("用户:{} 登录失败,异常:{}", username, authenticationException.getLocalizedMessage());
|
||||
}
|
||||
|
||||
}
|
||||
+4
-3
@@ -41,16 +41,16 @@ import javax.servlet.http.HttpServletResponse;
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class PigxAuthenticationSuccessEventHandler implements AuthenticationSuccessHandler {
|
||||
|
||||
private final RemoteLogService logService;
|
||||
|
||||
/**
|
||||
* 处理登录成功方法
|
||||
* <p>
|
||||
* 获取到登录的authentication 对象
|
||||
*
|
||||
* @param authentication 登录对象
|
||||
* @param request 请求
|
||||
* @param response 返回
|
||||
* @param request 请求
|
||||
* @param response 返回
|
||||
*/
|
||||
@Async
|
||||
@Override
|
||||
@@ -65,4 +65,5 @@ public class PigxAuthenticationSuccessEventHandler implements AuthenticationSucc
|
||||
logService.saveLog(sysLog, SecurityConstants.FROM_IN);
|
||||
log.info("用户:{} 登录成功", username);
|
||||
}
|
||||
|
||||
}
|
||||
+4
-4
@@ -19,14 +19,13 @@ import javax.sql.DataSource;
|
||||
*/
|
||||
@Service
|
||||
public class PigxClientDetailsServiceImpl extends JdbcClientDetailsService {
|
||||
|
||||
public PigxClientDetailsServiceImpl(DataSource dataSource) {
|
||||
super(dataSource);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 重写原生方法支持redis缓存
|
||||
*
|
||||
* @param clientId
|
||||
* @return ClientDetails
|
||||
* @throws InvalidClientException
|
||||
@@ -34,8 +33,9 @@ public class PigxClientDetailsServiceImpl extends JdbcClientDetailsService {
|
||||
@Override
|
||||
@Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null")
|
||||
public ClientDetails loadClientByClientId(String clientId) {
|
||||
super.setSelectClientDetailsSql(String.format(SecurityConstants.DEFAULT_SELECT_STATEMENT
|
||||
, TenantContextHolder.getTenantId()));
|
||||
super.setSelectClientDetailsSql(
|
||||
String.format(SecurityConstants.DEFAULT_SELECT_STATEMENT, TenantContextHolder.getTenantId()));
|
||||
return super.loadClientByClientId(clientId);
|
||||
}
|
||||
|
||||
}
|
||||
+8
-10
@@ -53,12 +53,13 @@ import java.util.Set;
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
|
||||
private final RemoteUserService remoteUserService;
|
||||
|
||||
private final CacheManager cacheManager;
|
||||
|
||||
/**
|
||||
* 用户密码登录
|
||||
*
|
||||
* @param username 用户名
|
||||
* @return
|
||||
* @throws UsernameNotFoundException
|
||||
@@ -77,10 +78,8 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据社交登录code 登录
|
||||
*
|
||||
* @param inStr TYPE@CODE
|
||||
* @return UserDetails
|
||||
* @throws UsernameNotFoundException
|
||||
@@ -93,7 +92,6 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
|
||||
/**
|
||||
* 构建userdetails
|
||||
*
|
||||
* @param result 用户信息
|
||||
* @return
|
||||
*/
|
||||
@@ -111,15 +109,15 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
|
||||
dbAuthsSet.addAll(Arrays.asList(info.getPermissions()));
|
||||
|
||||
}
|
||||
Collection<? extends GrantedAuthority> authorities
|
||||
= AuthorityUtils.createAuthorityList(dbAuthsSet.toArray(new String[0]));
|
||||
Collection<? extends GrantedAuthority> authorities = AuthorityUtils
|
||||
.createAuthorityList(dbAuthsSet.toArray(new String[0]));
|
||||
SysUser user = info.getSysUser();
|
||||
boolean enabled = StrUtil.equals(user.getLockFlag(), CommonConstants.STATUS_NORMAL);
|
||||
// 构造security用户
|
||||
|
||||
return new PigxUser(user.getUserId(), user.getDeptId(), user.getPhone(), user.getAvatar(), user.getTenantId()
|
||||
, user.getUsername(), SecurityConstants.BCRYPT + user.getPassword()
|
||||
, enabled, true, true
|
||||
, !CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities);
|
||||
return new PigxUser(user.getUserId(), user.getDeptId(), user.getPhone(), user.getAvatar(), user.getTenantId(),
|
||||
user.getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled, true, true,
|
||||
!CommonConstants.STATUS_LOCK.equals(user.getLockFlag()), authorities);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user