♻️ 重构代码。优化代码逻辑,方便扩展资源服务器

This commit is contained in:
冷冷 committed 2018-08-21 20:28:52 +08:00
1 parent e5364ad202
commit 336f1b944e
25 files changed
+208 -99

No files matched your search

+6
View File
@@ -46,6 +46,12 @@
<artifactId>pigx-upms-api</artifactId>
<version>${pigx.version}</version>
</dependency>
<!--security-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-security</artifactId>
<version>${pigx.version}</version>
</dependency>
<!--spring security 、oauth、jwt依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
@@ -22,7 +22,7 @@ package com.pig4cloud.pigx.auth.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pig4cloud.pigx.common.security.mobile.MobileLoginSuccessHandler;
import com.pig4cloud.pigx.common.security.mobile.MobileSecurityConfigurer;
import com.pig4cloud.pigx.common.security.util.PigxUserDetailsService;
import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+9 -1
View File
@@ -50,7 +50,6 @@
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!--jacksonJSR310规范-->
<dependency>
@@ -64,5 +63,14 @@
<version>${mbp.boot.version}</version>
<scope>compile</scope>
</dependency>
<!--feign 依赖-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
+5
View File
@@ -47,5 +47,10 @@
<artifactId>pigx-upms-api</artifactId>
<version>${pigx.version}</version>
</dependency>
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-security</artifactId>
<version>${pigx.version}</version>
</dependency>
</dependencies>
</project>
@@ -53,7 +53,7 @@ public class SysLogUtils {
});
SysLog sysLog = new SysLog();
sysLog.setCreateBy(SecurityUtils.getUser());
sysLog.setCreateBy(SecurityUtils.getUser().getUsername());
sysLog.setType(CommonConstant.STATUS_NORMAL);
sysLog.setRemoteAddr(HttpUtil.getClientIP(request));
sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI()));
+4 -7
View File
@@ -46,14 +46,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<!--feign 依赖-->
<!--UPMS API-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-upms-api</artifactId>
<version>${pigx.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.security.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
/**
* @author lengleng
* @date 2018/6/22
*/
public abstract class PigxResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {
@Autowired
protected ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;
@Autowired
protected PigAccessDeniedHandler pigAccessDeniedHandler;
@Autowired
protected RemoteTokenServices remoteTokenServices;
@Autowired
protected UserDetailsService userDetailsService;
@Override
public abstract void configure(HttpSecurity http) throws Exception;
/**
* why add resourceId
* https://stackoverflow.com/questions/28703847/how-do-you-set-a-resource-id-for-a-token
*
* @param resources
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
DefaultUserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
userTokenConverter.setUserDetailsService(userDetailsService);
accessTokenConverter.setUserTokenConverter(userTokenConverter);
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
.accessDeniedHandler(pigAccessDeniedHandler)
.tokenServices(remoteTokenServices);
}
}
@@ -17,7 +17,7 @@
package com.pig4cloud.pigx.common.security.mobile;
import com.pig4cloud.pigx.common.security.util.PigxUserDetailsService;
import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@@ -17,7 +17,7 @@
package com.pig4cloud.pigx.common.security.mobile;
import com.pig4cloud.pigx.common.security.util.PigxUserDetailsService;
import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
import lombok.AllArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.security.service;
import lombok.Getter;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import java.util.Collection;
/**
* @author lengleng
* @date 2018/8/20
* 扩展用户信息
*/
public class PigxUser extends User {
/**
* 用户ID
*/
@Getter
private Integer id;
/**
* 部门ID
*/
@Getter
private Integer deptId;
/**
* Construct the <code>User</code> with the details required by
* {@link DaoAuthenticationProvider}.
*
* @param id 用户ID
* @param deptId 部门ID
* @param username the username presented to the
* <code>DaoAuthenticationProvider</code>
* @param password the password that should be presented to the
* <code>DaoAuthenticationProvider</code>
* @param enabled set to <code>true</code> if the user is enabled
* @param accountNonExpired set to <code>true</code> if the account has not expired
* @param credentialsNonExpired set to <code>true</code> if the credentials have not
* expired
* @param accountNonLocked set to <code>true</code> if the account is not locked
* @param authorities the authorities that should be granted to the caller if they
* presented the correct username and password and the user is enabled. Not null.
* @throws IllegalArgumentException if a <code>null</code> value was passed either as
* a parameter or as an element in the <code>GrantedAuthority</code> collection
*/
public PigxUser(Integer id, Integer deptId, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
this.id = id;
this.deptId = id;
}
}
@@ -13,7 +13,8 @@
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/package com.pig4cloud.pigx.common.security.util;
*/
package com.pig4cloud.pigx.common.security.service;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
@@ -1,23 +1,21 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.auth.service;
package com.pig4cloud.pigx.common.security.service;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
@@ -27,12 +25,10 @@ import com.pig4cloud.pigx.admin.api.feign.RemoteUserService;
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.security.util.PigxUserDetailsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@@ -104,7 +100,8 @@ public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
SysUser user = info.getSysUser();
boolean enabled = StrUtil.equals(user.getDelFlag(), CommonConstant.STATUS_NORMAL);
// 构造security用户
return new User(info.getSysUser().getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled,
return new PigxUser(user.getUserId(), user.getDeptId(), user.getUsername(), SecurityConstants.BCRYPT + user.getPassword(), enabled,
true, true, true, authorities);
}
}
@@ -22,6 +22,7 @@ package com.pig4cloud.pigx.common.security.util;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
@@ -48,12 +49,12 @@ public class SecurityUtils {
/**
* 获取用户
*/
public static String getUser(Authentication authentication) {
public static PigxUser getUser(Authentication authentication) {
Object principal = authentication.getPrincipal();
if (principal == null) {
return null;
if (principal instanceof PigxUser) {
return (PigxUser) principal;
}
return (String) principal;
return null;
}
public static String getClientId() {
@@ -68,7 +69,7 @@ public class SecurityUtils {
/**
* 获取用户
*/
public static String getUser() {
public static PigxUser getUser() {
Authentication authentication = getAuthentication();
if (authentication == null) {
return null;
@@ -3,5 +3,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pigx.common.security.component.PigAccessDeniedHandler,\
com.pig4cloud.pigx.common.security.component.ResourceAuthExceptionEntryPoint,\
com.pig4cloud.pigx.common.security.feign.PigxFeignClientConfiguration,\
com.pig4cloud.pigx.common.security.service.PigxUserDetailsServiceImpl,\
com.pig4cloud.pigx.common.security.social.QqSocialConfig,\
com.pig4cloud.pigx.common.security.social.WxSocialConfig
-6
View File
@@ -41,12 +41,6 @@
<artifactId>pigx-common-security</artifactId>
<version>${pigx.version}</version>
</dependency>
<!--server-api-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!--swagger 依赖-->
<dependency>
<groupId>io.springfox</groupId>
+1 -1
View File
@@ -38,7 +38,7 @@
<!--core 工具类-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-security</artifactId>
<artifactId>pigx-common-core</artifactId>
<version>${pigx.version}</version>
</dependency>
</dependencies>
@@ -31,8 +31,8 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
* 用户统一管理系统
*/
@EnablePigxSwagger2
@EnableFeignClients
@SpringCloudApplication
@EnableFeignClients({"com.pig4cloud.pigx.admin.api.feign"})
public class PigxAdminApplication {
public static void main(String[] args) {
SpringApplication.run(PigxAdminApplication.class, args);
@@ -19,15 +19,11 @@
package com.pig4cloud.pigx.admin.config;
import com.pig4cloud.pigx.common.security.component.PigAccessDeniedHandler;
import com.pig4cloud.pigx.common.security.component.ResourceAuthExceptionEntryPoint;
import lombok.AllArgsConstructor;
import com.pig4cloud.pigx.common.security.component.PigxResourceServerConfigurerAdapter;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
/**
* @author lengleng
@@ -35,12 +31,8 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.Res
*/
@Configuration
@EnableResourceServer
@AllArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
private final PigAccessDeniedHandler pigAccessDeniedHandler;
private final ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;
public class ResourceServerConfigurer extends PigxResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
@@ -53,16 +45,5 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
.and().csrf().disable();
}
/**
* why add resourceId
* https://stackoverflow.com/questions/28703847/how-do-you-set-a-resource-id-for-a-token
*
* @param resources
* @throws Exception
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
.accessDeniedHandler(pigAccessDeniedHandler);
}
}
@@ -25,7 +25,7 @@ import com.pig4cloud.pigx.admin.api.entity.SysDept;
import com.pig4cloud.pigx.admin.service.SysDeptService;
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
import com.pig4cloud.pigx.common.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
@@ -42,9 +42,9 @@ import java.util.List;
*/
@RestController
@RequestMapping("/dept")
@AllArgsConstructor
public class DeptController {
@Autowired
private SysDeptService sysDeptService;
private final SysDeptService sysDeptService;
/**
* 通过ID查询
@@ -73,7 +73,7 @@ public class UserController {
}
//为空时查询当前用户
if (StrUtil.isBlank(username)) {
username = SecurityUtils.getUser();
username = SecurityUtils.getUser().getUsername();
}
return new R<>(userService.findUserInfo(EnumLoginType.PWD.getType(), username));
@@ -163,6 +163,6 @@ public class UserController {
*/
@PutMapping("/editInfo")
public R<Boolean> editInfo(@RequestBody UserDTO userDto) {
return userService.updateUserInfo(userDto, SecurityUtils.getUser());
return userService.updateUserInfo(userDto, SecurityUtils.getUser().getUsername());
}
}
@@ -29,8 +29,8 @@ import com.pig4cloud.pigx.admin.mapper.SysDeptMapper;
import com.pig4cloud.pigx.admin.mapper.SysDeptRelationMapper;
import com.pig4cloud.pigx.admin.service.SysDeptService;
import com.pig4cloud.pigx.common.core.constant.CommonConstant;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
@@ -46,11 +46,10 @@ import java.util.List;
* @since 2018-01-20
*/
@Service
@AllArgsConstructor
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
@Autowired
private SysDeptMapper sysDeptMapper;
@Autowired
private SysDeptRelationMapper sysDeptRelationMapper;
private final SysDeptMapper sysDeptMapper;
private final SysDeptRelationMapper sysDeptRelationMapper;
/**
* 添加信息部门
@@ -71,9 +71,7 @@ public class SysSocialDetailsServiceImpl extends ServiceImpl<SysSocialDetailsMap
public Boolean bindSocial(String appId, String code) {
Map<String, String> result = getOpenId(appId, code);
SysUser condition = new SysUser();
condition.setUsername(SecurityUtils.getUser());
SysUser sysUser = sysUserMapper.selectOne(condition);
SysUser sysUser = sysUserMapper.selectById(SecurityUtils.getUser().getId());
sysUser.setWxOpenid(result.get("openId"));
sysUserMapper.updateAllColumnById(sysUser);
@@ -17,14 +17,12 @@
package com.pig4cloud.pigx.codegen.config;
import com.pig4cloud.pigx.common.security.component.PigAccessDeniedHandler;
import com.pig4cloud.pigx.common.security.component.ResourceAuthExceptionEntryPoint;
import com.pig4cloud.pigx.common.security.component.PigxResourceServerConfigurerAdapter;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
/**
@@ -35,9 +33,7 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.Res
@EnableResourceServer
@AllArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
private final PigAccessDeniedHandler pigAccessDeniedHandler;
private final ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;
public class ResourceServerConfigurer extends PigxResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
@@ -48,14 +44,12 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
}
/**
* why add resourceId
* https://stackoverflow.com/questions/28703847/how-do-you-set-a-resource-id-for-a-token
* 重写抽象类实现,不需要调用feign 获取 userdetils
*
* @param resources
* @throws Exception
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
public void configure(ResourceServerSecurityConfigurer resources) {
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
.accessDeniedHandler(pigAccessDeniedHandler);
}
@@ -18,6 +18,7 @@
package com.pig4cloud.pigx.daemon.config;
import com.pig4cloud.pigx.common.security.component.PigAccessDeniedHandler;
import com.pig4cloud.pigx.common.security.component.PigxResourceServerConfigurerAdapter;
import com.pig4cloud.pigx.common.security.component.ResourceAuthExceptionEntryPoint;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Configuration;
@@ -35,9 +36,7 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.Res
@EnableResourceServer
@AllArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
private final PigAccessDeniedHandler pigAccessDeniedHandler;
private final ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;
public class ResourceServerConfigurer extends PigxResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
@@ -48,14 +47,12 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
}
/**
* why add resourceId
* https://stackoverflow.com/questions/28703847/how-do-you-set-a-resource-id-for-a-token
* 重写抽象类实现,不需要调用feign 获取 userdetils
*
* @param resources
* @throws Exception
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
public void configure(ResourceServerSecurityConfigurer resources) {
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
.accessDeniedHandler(pigAccessDeniedHandler);
}
-4
View File
@@ -67,10 +67,6 @@
<artifactId>pigx-common-core</artifactId>
<version>${pigx.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>