feat(新增): 🚀 安全框架
This commit is contained in:
parent
82693a8b0b
commit
2621e03020
|
@ -7,6 +7,7 @@
|
|||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="spring-security" />
|
||||
<module name="model" />
|
||||
<module name="common-util" />
|
||||
<module name="service-oa" />
|
||||
|
@ -22,6 +23,7 @@
|
|||
<module name="model" options="-parameters" />
|
||||
<module name="service-oa" options="-parameters" />
|
||||
<module name="service-util" options="-parameters" />
|
||||
<module name="spring-security" options="-parameters" />
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
|
@ -7,6 +7,7 @@
|
|||
<file url="file://$PROJECT_DIR$/common/service-util/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/common/service-util/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/common/service-util/src/main/resources-filtered" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/common/spring-security/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/common/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/common/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/common/src/main/resources-filtered" charset="UTF-8" />
|
||||
|
|
|
@ -14,7 +14,7 @@ public enum ResultCodeEnum {
|
|||
DATA_ERROR(204, "数据异常"),
|
||||
|
||||
LOGIN_AUTH(208, "未登陆"),
|
||||
PERMISSION(209, "没有权限");
|
||||
PERMISSION(209, "没有权限"), LOGIN_MOBLE_ERROR(204, "登录错误");
|
||||
|
||||
private final Integer code;
|
||||
private final String message;
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.atguigu.common.utlis;
|
||||
|
||||
import com.atguigu.common.result.Result;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ResponseUtil {
|
||||
|
||||
public static void out(HttpServletResponse response, Result r) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
response.setStatus(HttpStatus.OK.value());
|
||||
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
|
||||
try {
|
||||
mapper.writeValue(response.getWriter(), r);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.atguigu</groupId>
|
||||
<artifactId>common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-security</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-security</name>
|
||||
<url>https://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.atguigu</groupId>
|
||||
<artifactId>common-util</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!-- Spring Security依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,16 @@
|
|||
package com.atguigu.security.custom;
|
||||
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
@Component
|
||||
public class CustomMd5PasswordEncoder implements PasswordEncoder {
|
||||
public String encode(CharSequence rawPassword) {
|
||||
return DigestUtils.md5DigestAsHex(rawPassword.toString().getBytes());
|
||||
}
|
||||
|
||||
public boolean matches(CharSequence rawPassword, String encodedPassword) {
|
||||
return encodedPassword.equals(DigestUtils.md5DigestAsHex(rawPassword.toString().getBytes()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.atguigu.security.custom;
|
||||
|
||||
import com.atguigu.model.system.SysUser;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class CustomUser extends User {
|
||||
/**
|
||||
* 我们自己的用户实体对象,要调取用户信息时直接获取这个实体对象。(这里我就不写get/set方法了)
|
||||
*/
|
||||
private SysUser sysUser;
|
||||
|
||||
public CustomUser(SysUser sysUser, Collection<? extends GrantedAuthority> authorities) {
|
||||
super(sysUser.getUsername(), sysUser.getPassword(), authorities);
|
||||
this.sysUser = sysUser;
|
||||
}
|
||||
|
||||
public SysUser getSysUser() {
|
||||
return sysUser;
|
||||
}
|
||||
|
||||
public void setSysUser(SysUser sysUser) {
|
||||
this.sysUser = sysUser;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.atguigu.security.custom;
|
||||
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
|
||||
public interface UserDetailsService {
|
||||
/**
|
||||
* 根据用户名获取用户对象(获取不到直接抛异常)
|
||||
*/
|
||||
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.atguigu.security.fillter;
|
||||
|
||||
import com.atguigu.common.result.Result;
|
||||
import com.atguigu.common.result.ResultCodeEnum;
|
||||
import com.atguigu.common.utlis.JwtHelper;
|
||||
import com.atguigu.common.utlis.ResponseUtil;
|
||||
import com.atguigu.security.custom.CustomUser;
|
||||
import com.atguigu.vo.system.LoginVo;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 登录过滤器,继承UsernamePasswordAuthenticationFilter,对用户名密码进行登录校验
|
||||
* </p>
|
||||
*/
|
||||
public class TokenAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
|
||||
public TokenAuthenticationFilter(AuthenticationManager authenticationManager) {
|
||||
this.setAuthenticationManager(authenticationManager);
|
||||
this.setPostOnly(false);
|
||||
// 指定登录接口及提交方式,可以指定任意路径
|
||||
this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/admin/system/index/login", "POST"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录认证
|
||||
*/
|
||||
@Override
|
||||
public Authentication attemptAuthentication(HttpServletRequest req, HttpServletResponse res)
|
||||
throws AuthenticationException {
|
||||
try {
|
||||
LoginVo loginVo = new ObjectMapper().readValue(req.getInputStream(), LoginVo.class);
|
||||
|
||||
Authentication authenticationToken = new UsernamePasswordAuthenticationToken(loginVo.getUsername(), loginVo.getPassword());
|
||||
return this.getAuthenticationManager().authenticate(authenticationToken);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录成功
|
||||
*/
|
||||
@Override
|
||||
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
|
||||
Authentication auth) throws IOException, ServletException {
|
||||
CustomUser customUser = (CustomUser) auth.getPrincipal();
|
||||
String token = JwtHelper.createToken(customUser.getSysUser().getId(), customUser.getSysUser().getUsername());
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("token", token);
|
||||
ResponseUtil.out(response, Result.success(map));
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录失败
|
||||
*/
|
||||
@Override
|
||||
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
|
||||
AuthenticationException e) {
|
||||
if (e.getCause() instanceof RuntimeException) {
|
||||
ResponseUtil.out(response, Result.error(null, 204, e.getMessage()));
|
||||
} else {
|
||||
ResponseUtil.out(response, Result.error(null, ResultCodeEnum.LOGIN_MOBLE_ERROR));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -305,3 +305,103 @@
|
|||
14:15:45:080 INFO 20408 --- [http-nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
|
||||
14:15:45:084 INFO 20408 --- [http-nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
|
||||
14:16:25:662 INFO 20408 --- [SpringContextShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
|
||||
14:16:28:044 INFO 13272 --- [main] com.atguigu.auth.ServiceAuthApplication : Starting ServiceAuthApplication on Bunny with PID 13272 (F:\java项目\guigu-oa\guigu-oa\service-oa\target\classes started by ACE in F:\java项目\guigu-oa\guigu-oa)
|
||||
14:16:28:046 INFO 13272 --- [main] com.atguigu.auth.ServiceAuthApplication : The following profiles are active: dev
|
||||
14:16:28:861 INFO 13272 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8800 (http)
|
||||
14:16:28:869 INFO 13272 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
|
||||
14:16:28:869 INFO 13272 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
|
||||
14:16:28:931 INFO 13272 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
|
||||
14:16:28:932 INFO 13272 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 851 ms
|
||||
14:16:29:032 INFO 13272 --- [main] com.atguigu.config.MybatisPlusConfig : 注入MybatisPlus配置类...
|
||||
14:16:29:602 INFO 13272 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
|
||||
14:16:29:743 INFO 13272 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8800 (http) with context path ''
|
||||
14:16:30:017 INFO 13272 --- [main] com.atguigu.auth.ServiceAuthApplication : Started ServiceAuthApplication in 2.232 seconds (JVM running for 2.626)
|
||||
14:16:33:582 INFO 13272 --- [http-nio-8800-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
|
||||
14:16:33:582 INFO 13272 --- [http-nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
|
||||
14:16:33:585 INFO 13272 --- [http-nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
|
||||
14:16:33:615 INFO 13272 --- [http-nio-8800-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
|
||||
14:16:33:850 INFO 13272 --- [http-nio-8800-exec-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
|
||||
14:16:33:908 ERROR 13272 --- [http-nio-8800-exec-1] c.a.exception.GlobalExceptionHandler : GlobalExceptionHandler===>自定义异常信息:用户不存在
|
||||
14:49:35:578 INFO 13272 --- [SpringContextShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
|
||||
14:49:35:579 INFO 13272 --- [SpringContextShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
|
||||
14:49:35:581 INFO 13272 --- [SpringContextShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
|
||||
14:49:40:028 INFO 26124 --- [main] com.atguigu.auth.ServiceAuthApplication : Starting ServiceAuthApplication on Bunny with PID 26124 (F:\java项目\guigu-oa\guigu-oa\service-oa\target\classes started by ACE in F:\java项目\guigu-oa\guigu-oa)
|
||||
14:49:40:030 INFO 26124 --- [main] com.atguigu.auth.ServiceAuthApplication : The following profiles are active: dev
|
||||
14:49:40:939 INFO 26124 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8800 (http)
|
||||
14:49:40:950 INFO 26124 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
|
||||
14:49:40:951 INFO 26124 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
|
||||
14:49:41:016 INFO 26124 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
|
||||
14:49:41:016 INFO 26124 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 948 ms
|
||||
14:49:41:117 INFO 26124 --- [main] com.atguigu.config.MybatisPlusConfig : 注入MybatisPlus配置类...
|
||||
14:49:41:710 INFO 26124 --- [main] .s.s.UserDetailsServiceAutoConfiguration :
|
||||
|
||||
Using generated security password: 22e6691e-e731-4b1b-bbb7-49c861afe133
|
||||
|
||||
14:49:41:792 INFO 26124 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@2e1ba142, org.springframework.security.web.context.SecurityContextPersistenceFilter@1cc93da4, org.springframework.security.web.header.HeaderWriterFilter@35e689a0, org.springframework.security.web.csrf.CsrfFilter@280d5a82, org.springframework.security.web.authentication.logout.LogoutFilter@19647566, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@7769d9b6, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@273293c8, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@abc7005, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@7878bbdb, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@7e38e254, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@17136390, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@595ec862, org.springframework.security.web.session.SessionManagementFilter@3bf5911d, org.springframework.security.web.access.ExceptionTranslationFilter@64f3991e, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@3b33fff9]
|
||||
14:49:41:845 INFO 26124 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
|
||||
14:49:41:999 INFO 26124 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8800 (http) with context path ''
|
||||
14:49:42:269 INFO 26124 --- [main] com.atguigu.auth.ServiceAuthApplication : Started ServiceAuthApplication in 2.505 seconds (JVM running for 2.966)
|
||||
14:51:02:789 INFO 26124 --- [http-nio-8800-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
|
||||
14:51:02:789 INFO 26124 --- [http-nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
|
||||
14:51:02:793 INFO 26124 --- [http-nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
|
||||
14:52:05:584 INFO 26124 --- [SpringContextShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
|
||||
14:52:08:294 INFO 28940 --- [main] com.atguigu.auth.ServiceAuthApplication : Starting ServiceAuthApplication on Bunny with PID 28940 (F:\java项目\guigu-oa\guigu-oa\service-oa\target\classes started by ACE in F:\java项目\guigu-oa\guigu-oa)
|
||||
14:52:08:296 INFO 28940 --- [main] com.atguigu.auth.ServiceAuthApplication : The following profiles are active: dev
|
||||
14:52:09:241 INFO 28940 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8800 (http)
|
||||
14:52:09:248 INFO 28940 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
|
||||
14:52:09:248 INFO 28940 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
|
||||
14:52:09:312 INFO 28940 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
|
||||
14:52:09:312 INFO 28940 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 977 ms
|
||||
14:52:09:429 INFO 28940 --- [main] com.atguigu.config.MybatisPlusConfig : 注入MybatisPlus配置类...
|
||||
14:52:10:030 INFO 28940 --- [main] .s.s.UserDetailsServiceAutoConfiguration :
|
||||
|
||||
Using generated security password: ae2ed156-c680-449d-ad9e-71583c31ef54
|
||||
|
||||
14:52:10:100 INFO 28940 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@53e28097, org.springframework.security.web.context.SecurityContextPersistenceFilter@35e689a0, org.springframework.security.web.header.HeaderWriterFilter@21a5b599, org.springframework.security.web.csrf.CsrfFilter@59f3426f, org.springframework.security.web.authentication.logout.LogoutFilter@3041beb3, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@fd87c22, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@6dcbbb49, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@6e794f53, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@4ca0b9b1, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@3bf5911d, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@69a90b81, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@75f4d8a8, org.springframework.security.web.session.SessionManagementFilter@64f3991e, org.springframework.security.web.access.ExceptionTranslationFilter@1de08775, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@500ec769]
|
||||
14:52:10:151 INFO 28940 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
|
||||
14:52:10:297 INFO 28940 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8800 (http) with context path ''
|
||||
14:52:10:570 INFO 28940 --- [main] com.atguigu.auth.ServiceAuthApplication : Started ServiceAuthApplication in 2.611 seconds (JVM running for 3.178)
|
||||
14:52:12:610 INFO 28940 --- [http-nio-8800-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
|
||||
14:52:12:611 INFO 28940 --- [http-nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
|
||||
14:52:12:614 INFO 28940 --- [http-nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
|
||||
14:53:24:672 INFO 28940 --- [SpringContextShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
|
||||
14:53:27:344 INFO 9704 --- [main] com.atguigu.auth.ServiceAuthApplication : Starting ServiceAuthApplication on Bunny with PID 9704 (F:\java项目\guigu-oa\guigu-oa\service-oa\target\classes started by ACE in F:\java项目\guigu-oa\guigu-oa)
|
||||
14:53:27:346 INFO 9704 --- [main] com.atguigu.auth.ServiceAuthApplication : The following profiles are active: dev
|
||||
14:53:28:360 INFO 9704 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8800 (http)
|
||||
14:53:28:367 INFO 9704 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
|
||||
14:53:28:367 INFO 9704 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
|
||||
14:53:28:444 INFO 9704 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
|
||||
14:53:28:445 INFO 9704 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1046 ms
|
||||
14:53:28:560 INFO 9704 --- [main] com.atguigu.config.MybatisPlusConfig : 注入MybatisPlus配置类...
|
||||
14:53:29:142 INFO 9704 --- [main] .s.s.UserDetailsServiceAutoConfiguration :
|
||||
|
||||
Using generated security password: 8096bafc-491c-41f3-80b4-f6829ee2cb99
|
||||
|
||||
14:53:29:209 INFO 9704 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@102af1bb, org.springframework.security.web.context.SecurityContextPersistenceFilter@2e4ecdf8, org.springframework.security.web.header.HeaderWriterFilter@1cc93da4, org.springframework.security.web.csrf.CsrfFilter@6efd0a6e, org.springframework.security.web.authentication.logout.LogoutFilter@2335aef2, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@5af38a4a, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@333e5fb6, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@797f97e3, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@6e794f53, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@69a90b81, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@a120b9, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@56cd5d76, org.springframework.security.web.session.SessionManagementFilter@7e38e254, org.springframework.security.web.access.ExceptionTranslationFilter@3bf5911d, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@3b9c9b8b]
|
||||
14:53:29:261 INFO 9704 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
|
||||
14:53:29:412 INFO 9704 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8800 (http) with context path ''
|
||||
14:53:29:684 INFO 9704 --- [main] com.atguigu.auth.ServiceAuthApplication : Started ServiceAuthApplication in 2.756 seconds (JVM running for 3.209)
|
||||
14:53:47:436 INFO 9704 --- [http-nio-8800-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
|
||||
14:53:47:437 INFO 9704 --- [http-nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
|
||||
14:53:47:440 INFO 9704 --- [http-nio-8800-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
|
||||
14:54:12:750 INFO 9704 --- [SpringContextShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
|
||||
14:54:14:216 INFO 16348 --- [main] com.atguigu.auth.ServiceAuthApplication : Starting ServiceAuthApplication on Bunny with PID 16348 (F:\java项目\guigu-oa\guigu-oa\service-oa\target\classes started by ACE in F:\java项目\guigu-oa\guigu-oa)
|
||||
14:54:14:218 INFO 16348 --- [main] com.atguigu.auth.ServiceAuthApplication : The following profiles are active: dev
|
||||
14:54:15:134 INFO 16348 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8800 (http)
|
||||
14:54:15:141 INFO 16348 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
|
||||
14:54:15:142 INFO 16348 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39]
|
||||
14:54:15:202 INFO 16348 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
|
||||
14:54:15:202 INFO 16348 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 947 ms
|
||||
14:54:15:319 INFO 16348 --- [main] com.atguigu.config.MybatisPlusConfig : 注入MybatisPlus配置类...
|
||||
14:54:15:938 INFO 16348 --- [main] .s.s.UserDetailsServiceAutoConfiguration :
|
||||
|
||||
Using generated security password: c1b9b421-40ec-420f-88cd-c249c5d26684
|
||||
|
||||
14:54:16:006 INFO 16348 --- [main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@75f4d8a8, org.springframework.security.web.context.SecurityContextPersistenceFilter@3bf5911d, org.springframework.security.web.header.HeaderWriterFilter@64f3991e, org.springframework.security.web.csrf.CsrfFilter@6cc8adff, org.springframework.security.web.authentication.logout.LogoutFilter@2e40fdbd, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@1ff463bb, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@36c783ca, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@69ba72da, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@1be77a76, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1cc93da4, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@35329a05, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2e1ba142, org.springframework.security.web.session.SessionManagementFilter@35e689a0, org.springframework.security.web.access.ExceptionTranslationFilter@21a5b599, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@7bce9ce4]
|
||||
14:54:16:058 INFO 16348 --- [main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
|
||||
14:54:16:203 INFO 16348 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8800 (http) with context path ''
|
||||
14:54:16:485 INFO 16348 --- [main] com.atguigu.auth.ServiceAuthApplication : Started ServiceAuthApplication in 2.538 seconds (JVM running for 2.963)
|
||||
14:54:25:577 INFO 16348 --- [http-nio-8800-exec-4] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
|
||||
14:54:25:578 INFO 16348 --- [http-nio-8800-exec-4] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
|
||||
14:54:25:583 INFO 16348 --- [http-nio-8800-exec-4] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
|
||||
16:12:27:427 INFO 16348 --- [SpringContextShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
|
||||
|
|
1
pom.xml
1
pom.xml
|
@ -18,6 +18,7 @@
|
|||
<module>common</module>
|
||||
<module>model</module>
|
||||
<module>service-oa</module>
|
||||
<module>common/spring-security</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -28,6 +28,11 @@
|
|||
<artifactId>service-util</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.atguigu</groupId>
|
||||
<artifactId>spring-security</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -53,4 +53,12 @@ public interface SysUserService extends IService<SysUser> {
|
|||
* @return 用户信息
|
||||
*/
|
||||
SysUserinfo getUserinfo(HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* 根据用户名查询用户信息
|
||||
*
|
||||
* @param username 用户名
|
||||
* @return 用户信息
|
||||
*/
|
||||
SysUser getByUsername(String username);
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import com.atguigu.vo.system.RouterVo;
|
|||
import com.atguigu.vo.system.SysUserQueryVo;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -147,4 +148,15 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|||
.routers(routerVoList)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户名查询用户信息
|
||||
*
|
||||
* @param username 用户名
|
||||
* @return 用户信息
|
||||
*/
|
||||
@Override
|
||||
public SysUser getByUsername(String username) {
|
||||
return getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package com.atguigu.auth.service.impl;
|
||||
|
||||
import com.atguigu.auth.service.SysUserService;
|
||||
import com.atguigu.constant.MessageConstant;
|
||||
import com.atguigu.exception.BunnyException;
|
||||
import com.atguigu.model.system.SysUser;
|
||||
import com.atguigu.security.custom.CustomUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@Component
|
||||
public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
@Autowired
|
||||
private SysUserService sysUserService;
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
SysUser sysUser = sysUserService.getByUsername(username);
|
||||
if (null == sysUser) {
|
||||
throw new UsernameNotFoundException(MessageConstant.USER_DOES_NOT_EXIST);
|
||||
}
|
||||
|
||||
if (sysUser.getStatus() == 0) {
|
||||
throw new BunnyException(MessageConstant.ACCOUNT_LOCKED);
|
||||
}
|
||||
|
||||
return new CustomUser(sysUser, Collections.emptyList());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue