From 664b03e2c942afd0b34eb36759c829e68d51cfbc Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Wed, 18 Nov 2020 17:14:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- youlai-admin/admin-api/pom.xml | 2 +- youlai-auth/pom.xml | 2 +- .../youlai/auth/config/WebSecurityConfig.java | 53 ------------------- youlai-common/common-mybatis/pom.xml | 30 +++++++++++ .../mybatis/config/MybatisPlusConfig.java | 33 ++++++++++++ .../common/mybatis/handler/MetaHandler.java | 23 ++++++++ .../main/resources/META-INF/spring.factories | 2 + youlai-common/pom.xml | 4 +- 8 files changed, 92 insertions(+), 57 deletions(-) create mode 100644 youlai-common/common-mybatis/pom.xml create mode 100644 youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/config/MybatisPlusConfig.java create mode 100644 youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/MetaHandler.java create mode 100644 youlai-common/common-mybatis/src/main/resources/META-INF/spring.factories diff --git a/youlai-admin/admin-api/pom.xml b/youlai-admin/admin-api/pom.xml index cb37f688e..5a6728933 100644 --- a/youlai-admin/admin-api/pom.xml +++ b/youlai-admin/admin-api/pom.xml @@ -37,7 +37,7 @@ com.youlai - common-database + common-mybatis ${youlai.version} diff --git a/youlai-auth/pom.xml b/youlai-auth/pom.xml index 70d892b56..dc3e5629d 100644 --- a/youlai-auth/pom.xml +++ b/youlai-auth/pom.xml @@ -30,7 +30,7 @@ com.youlai - common-database + common-mybatis ${youlai.version} diff --git a/youlai-auth/src/main/java/com/youlai/auth/config/WebSecurityConfig.java b/youlai-auth/src/main/java/com/youlai/auth/config/WebSecurityConfig.java index 9262fc22e..bf7bd4ce9 100644 --- a/youlai-auth/src/main/java/com/youlai/auth/config/WebSecurityConfig.java +++ b/youlai-auth/src/main/java/com/youlai/auth/config/WebSecurityConfig.java @@ -31,33 +31,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) throws Exception { // 登录失败处理handler,返回一段json http - .formLogin().failureHandler( - (req, resp, e) -> { - resp.setContentType("application/json;charset=utf-8"); - PrintWriter out = resp.getWriter(); - Map map = new HashMap<>(); - map.put("status", 401); - if (e instanceof LockedException) { - map.put("msg", "账户被锁定,登录失败!"); - } else if (e instanceof BadCredentialsException) { - map.put("msg", "用户名或密码输入错误,登录失败!"); - } else if (e instanceof DisabledException) { - map.put("msg", "账户被禁用,登录失败!"); - } else if (e instanceof AccountExpiredException) { - map.put("msg", "账户过期,登录失败!"); - } else if (e instanceof CredentialsContainer) { - map.put("msg", "密码过期,登录失败"); - } else { - map.put("msg", "登录失败!"); - } - out.write(new ObjectMapper().writeValueAsString(map)); - out.flush(); - out.close(); - } - - - ) - .and() .authorizeRequests().requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll() .and() .authorizeRequests().antMatchers("/oauth/public_key", "/oauth/logout").permitAll().anyRequest().authenticated() @@ -74,32 +47,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { return super.authenticationManagerBean(); } - - @Bean - AuthenticationFailureHandler authenticationFailureHandler() { - return (request, response, e) -> { - - /* - if (!user.isEnabled()) { - throw new DisabledException("该账户已被禁用!"); - } else if (!user.isAccountNonLocked()) { - throw new LockedException("该账号已被锁定!"); - } else if (!user.isAccountNonExpired()) { - throw new AccountExpiredException("该账号已过期!"); - } else if (!user.isCredentialsNonExpired()) { - throw new CredentialsExpiredException("该账户的登录凭证已过期,请重新登录!"); - } -*/ - if (e instanceof DisabledException) { - log.info(e.getMessage()); - } else if (e instanceof LockedException) { - log.info(e.getMessage()); - } else if (e instanceof AccountExpiredException) { - log.info(e.getMessage()); - } - }; - } - @Bean public PasswordEncoder passwordEncoder() { return PasswordEncoderFactories.createDelegatingPasswordEncoder(); diff --git a/youlai-common/common-mybatis/pom.xml b/youlai-common/common-mybatis/pom.xml new file mode 100644 index 000000000..3cd1976fd --- /dev/null +++ b/youlai-common/common-mybatis/pom.xml @@ -0,0 +1,30 @@ + + + + youlai-common + com.youlai + 1.0.0-SNAPSHOT + + 4.0.0 + + common-mybatis + + + + mysql + mysql-connector-java + + + + com.alibaba + druid-spring-boot-starter + + + + com.baomidou + mybatis-plus-boot-starter + + + \ No newline at end of file diff --git a/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/config/MybatisPlusConfig.java b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/config/MybatisPlusConfig.java new file mode 100644 index 000000000..62a081884 --- /dev/null +++ b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/config/MybatisPlusConfig.java @@ -0,0 +1,33 @@ +package com.youlai.common.mybatis.config; + +import com.baomidou.mybatisplus.core.config.GlobalConfig; +import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; +import com.youlai.common.mybatis.handler.MetaHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@EnableTransactionManagement +public class MybatisPlusConfig { + + /** + * 分页插件,自动识别数据库类型 多租户,请参考官网【插件扩展】 + */ + @Bean + public PaginationInterceptor paginationInterceptor() { + return new PaginationInterceptor(); + } + + + /** + * 自动填充数据库创建人、创建时间、更新人、更新时间 + */ + @Bean + public GlobalConfig globalConfig() { + GlobalConfig globalConfig = new GlobalConfig(); + globalConfig.setMetaObjectHandler(new MetaHandler()); + return globalConfig; + } + +} diff --git a/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/MetaHandler.java b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/MetaHandler.java new file mode 100644 index 000000000..b05a0aec7 --- /dev/null +++ b/youlai-common/common-mybatis/src/main/java/com/youlai/common/mybatis/handler/MetaHandler.java @@ -0,0 +1,23 @@ +package com.youlai.common.mybatis.handler; + +import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; +import org.apache.ibatis.reflection.MetaObject; +import org.springframework.stereotype.Component; + +import java.util.Date; + +@Component +public class MetaHandler implements MetaObjectHandler { + + @Override + public void insertFill(MetaObject metaObject) { + this.setFieldValByName("gmtCreate", new Date(), metaObject); + this.setFieldValByName("gmtModified", new Date(), metaObject); + } + + @Override + public void updateFill(MetaObject metaObject) { + this.setFieldValByName("gmtCreate", new Date(), metaObject); + } + +} diff --git a/youlai-common/common-mybatis/src/main/resources/META-INF/spring.factories b/youlai-common/common-mybatis/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..36479e74c --- /dev/null +++ b/youlai-common/common-mybatis/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.youlai.common.mybatis.config.MybatisPlusConfig diff --git a/youlai-common/pom.xml b/youlai-common/pom.xml index 7931d42bc..2e612db26 100644 --- a/youlai-common/pom.xml +++ b/youlai-common/pom.xml @@ -18,7 +18,7 @@ common-knife4j common-redis common-web - common-database + common-mybatis @@ -118,7 +118,7 @@ com.youlai - common-database + common-mybatis ${youlai.version}