⚡ 优化代码,区别重写和默认情况

This commit is contained in:
冷冷 committed 2018-11-11 01:52:12 +08:00
1 parent 45ee93c0eb
commit 547ae5cd90
15 files changed
+81 -39

No files matched your search

@@ -19,6 +19,8 @@ package com.pig4cloud.pigx.common.security.annotation;
import com.pig4cloud.pigx.common.security.component.PigxResourceServerAutoConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import java.lang.annotation.*;
@@ -32,6 +34,8 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Import(PigxResourceServerAutoConfiguration.class)
public @interface EnablePigxResourceServer {
}
@@ -19,14 +19,9 @@ package com.pig4cloud.pigx.common.security.component;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
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.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
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;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
@@ -42,9 +37,6 @@ import org.springframework.web.client.RestTemplate;
* 2. 支持 获取用户全部信息
*/
@Slf4j
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {
@Autowired
protected ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;
@@ -56,6 +48,8 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
protected UserDetailsService userDetailsService;
@Autowired
private PermitAllUrlProperties permitAllUrlProperties;
@Autowired
private RestTemplate lbRestTemplate;
/**
@@ -92,12 +86,6 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
canGetUser(resources);
}
@Bean
@LoadBalanced
public RestTemplate lbRestTemplate() {
return new RestTemplate();
}
/**
* 不获取用户详细 只有用户名
@@ -109,7 +97,7 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
DefaultUserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
accessTokenConverter.setUserTokenConverter(userTokenConverter);
remoteTokenServices.setRestTemplate(lbRestTemplate());
remoteTokenServices.setRestTemplate(lbRestTemplate);
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
.accessDeniedHandler(pigAccessDeniedHandler)
@@ -128,7 +116,7 @@ public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigure
userTokenConverter.setUserDetailsService(userDetailsService);
accessTokenConverter.setUserTokenConverter(userTokenConverter);
remoteTokenServices.setRestTemplate(lbRestTemplate());
remoteTokenServices.setRestTemplate(lbRestTemplate);
remoteTokenServices.setAccessTokenConverter(accessTokenConverter);
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
.accessDeniedHandler(pigAccessDeniedHandler)
@@ -17,7 +17,13 @@
package com.pig4cloud.pigx.common.security.component;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.web.client.RestTemplate;
/**
* @author lengleng
@@ -25,4 +31,16 @@ import org.springframework.context.annotation.ComponentScan;
*/
@ComponentScan("com.pig4cloud.pigx.common.security")
public class PigxResourceServerAutoConfiguration {
@Bean
@Primary
@LoadBalanced
public RestTemplate lbRestTemplate() {
return new RestTemplate();
}
@Bean
@ConditionalOnMissingBean(ResourceServerConfigurerAdapter.class)
public ResourceServerConfigurerAdapter resourceServerConfigurerAdapter() {
return new BaseResourceServerConfigurerAdapter();
}
}
@@ -98,10 +98,6 @@ swagger:
## spring security 配置
security:
oauth2:
client:
ignore-urls:
- '/actuator/**'
- '/v2/api-docs'
resource:
loadBalanced: true
token-info-uri: http://pigx-auth/oauth/check_token
@@ -21,4 +21,4 @@ spring:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://pigx-mysql:3306/pigxx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
url: jdbc:mysql://pigx-mysql:3306/pigxx_ac?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
@@ -5,6 +5,9 @@ security:
client-id: ENC(gPFcUOmJm8WqM3k3eSqS0Q==)
client-secret: ENC(gPFcUOmJm8WqM3k3eSqS0Q==)
scope: server
ignore-urls:
- '/actuator/**'
- '/v2/api-docs'
# 数据源配置
spring:
@@ -5,7 +5,9 @@ security:
client-id: ENC(tz2NM4GcmnE7sNJTYL8ZSg==)
client-secret: ENC(tz2NM4GcmnE7sNJTYL8ZSg==)
scope: server
ignore-urls:
- '/actuator/**'
- '/v2/api-docs'
## 定时任务
spring:
# 保存定时任务的数据源
@@ -6,6 +6,8 @@ security:
client-secret: ENC(ltJPpR50wT0oIY9kfOe1Iw==)
scope: server
ignore-urls:
- '/actuator/**'
- '/v2/api-docs'
- '/user/info/*'
- '/social/info/**'
- '/log/**'
@@ -61,10 +61,8 @@ public interface RemoteUserService {
* 查询上级部门的用户信息
*
* @param username 用户名
* @param from 调用标志
* @return R
*/
@GetMapping("/user/ancestorUsers/{username}")
R<List<SysUser>> ancestorUsers(@PathVariable("username") String username
, @RequestHeader("from") String from);
R<List<SysUser>> ancestorUsers(@PathVariable("username") String username);
}
@@ -68,11 +68,10 @@ public class RemoteUserServiceFallbackImpl implements RemoteUserService {
* 查询上级部门的用户信息
*
* @param username 用户名
* @param from 调用标志
* @return R
*/
@Override
public R<List<SysUser>> ancestorUsers(String username, String from) {
public R<List<SysUser>> ancestorUsers(String username) {
log.error("feign 查询用户上级部门用户列失败:{}", username, cause);
return null;
}
@@ -18,15 +18,26 @@
package com.pig4cloud.pigx.act.config;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.parser.ISqlParser;
import com.baomidou.mybatisplus.plugins.parser.tenant.TenantHandler;
import com.baomidou.mybatisplus.plugins.parser.tenant.TenantSqlParser;
import com.pig4cloud.pigx.common.core.util.TenantUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* @author lengleng
* @date 2018/9/27
* mybatis-plus 配置
*/
@Slf4j
@Configuration
@MapperScan("com.pig4cloud.pigx.act.mapper")
public class MybatisPlusConfigurer {
@@ -37,6 +48,29 @@ public class MybatisPlusConfigurer {
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
List<ISqlParser> sqlParserList = new ArrayList<>();
TenantSqlParser tenantSqlParser = new TenantSqlParser();
tenantSqlParser.setTenantHandler(new TenantHandler() {
@Override
public Expression getTenantId() {
Integer tenantId = TenantUtils.getTenantId();
log.debug("当前租户为 >> {}", tenantId);
return new LongValue(tenantId);
}
@Override
public String getTenantIdColumn() {
return "tenant_id";
}
@Override
public boolean doTableFilter(String tableName) {
return false;
}
});
sqlParserList.add(tenantSqlParser);
paginationInterceptor.setSqlParserList(sqlParserList);
return paginationInterceptor;
}
}
@@ -26,7 +26,7 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.Res
* @date 2018/6/22
*/
@Configuration
public class ResourceServerConfigurer extends BaseResourceServerConfigurerAdapter {
public class ResourceServerConfigurer extends BaseResourceServerConfigurerAdapter {
/**
* 重写不需要调用feign 获取 userdetils
@@ -35,7 +35,6 @@ public class ResourceServerConfigurer extends BaseResourceServerConfigurerAdapt
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.authenticationEntryPoint(resourceAuthExceptionEntryPoint)
.accessDeniedHandler(pigAccessDeniedHandler);
notGetUser(resources);
}
}
@@ -62,7 +62,11 @@ public class LeaveBill extends Model<LeaveBill> {
*/
private String state;
/**
* 请假时间
* 提交时间
*/
private LocalDateTime leaveTime;
/**
* 提交时间
*/
private LocalDateTime createTime;
/**
@@ -20,7 +20,6 @@ package com.pig4cloud.pigx.act.listener;
import cn.hutool.core.collection.CollUtil;
import com.pig4cloud.pigx.admin.api.entity.SysUser;
import com.pig4cloud.pigx.admin.api.feign.RemoteUserService;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
@@ -48,8 +47,7 @@ public class LeaveProcessTaskListener implements TaskListener {
public void notify(DelegateTask delegateTask) {
RemoteUserService userService = SpringContextHolder.getBean(RemoteUserService.class);
R<List<SysUser>> result = userService.ancestorUsers(
SecurityUtils.getUsername(), SecurityConstants.FROM_IN);
R<List<SysUser>> result = userService.ancestorUsers(SecurityUtils.getUsername());
if (CollUtil.isEmpty(result.getData())) {
log.debug("用户 {} 不存在上级,任务单由当前用户审批", SecurityUtils.getUsername());
@@ -146,10 +146,7 @@ public class ActTaskServiceImpl implements ActTaskService {
taskService.addComment(taskId, processInstanceId, message);
Map<String, Object> variables = new HashMap<>(1);
if (!StrUtil.equals(FLAG, leaveBillDto.getTaskFlag())) {
variables.put("flag", leaveBillDto.getTaskFlag());
}
variables.put("flag", leaveBillDto.getTaskFlag());
taskService.complete(taskId, variables);
ProcessInstance pi = runtimeService.createProcessInstanceQuery()