Compare commits

...

2 Commits

Author SHA1 Message Date
adiao 221952de21 app fix 3 years ago
adiao 0bc36f8388 app fix 3 years ago
  1. 2
      README.md
  2. 43
      app-api/pom.xml
  3. 2
      app-dao/pom.xml
  4. 22
      app-dao/src/main/java/co/depsystem/app/demos/web/Author/TokenMap.java
  5. 4
      app-service/pom.xml
  6. 38
      app-service/src/main/java/co/depsystem/app/service/author/Impl/AuthorFilterImpl.java
  7. 81
      app-service/src/main/java/co/depsystem/app/service/author/Impl/LoginFilterImpl.java
  8. 6
      pom.xml
  9. 4
      start/src/main/java/co/depsystem/app/AppApplication.java
  10. 4
      start/src/test/java/co/depsystem/app/AppApplicationTests.java

2
README.md

@ -66,3 +66,5 @@ demo-manager 层不应该处理任何与数据读写无关的业务逻辑,这 @@ -66,3 +66,5 @@ demo-manager 层不应该处理任何与数据读写无关的业务逻辑,这
![拦截、过滤、AOP流程图](https://img2022.cnblogs.com/blog/1401949/202204/1401949-20220414161645657-1831824836.png)
请求进入是首先进入到过滤中,随后依次逐层进入。当碰到异常之后则是由内而外的抛出。
调用了setFilterProcessesUrl方法,设置过滤器处理的URL

43
app-api/pom.xml

@ -47,14 +47,52 @@ @@ -47,14 +47,52 @@
<version>3.0.4</version>
</dependency>
<!-- jwt -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.25</version>
</dependency>
<!-- jwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 修复不会自动添加Bind包的依赖 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<!-- 修复不会自动添加Bind包的依赖 -->
<!-- <dependency>-->
<!-- <groupId>javax.xml.bind</groupId>-->
<!-- <artifactId>jaxb-api</artifactId>-->
@ -76,6 +114,7 @@ @@ -76,6 +114,7 @@
<!-- <version>1.1.1</version>-->
<!-- </dependency>-->
</dependencies>
</project>

2
app-dao/pom.xml

@ -20,7 +20,7 @@ @@ -20,7 +20,7 @@
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.1</version>
<version>3.0.0</version>
</dependency>
<dependency>

22
app-dao/src/main/java/co/depsystem/app/demos/web/Author/TokenMap.java

@ -0,0 +1,22 @@ @@ -0,0 +1,22 @@
/**
* @author JOJO
* @class TokenMap
* @date 2023/4/12
* @apiNote
*/
package co.depsystem.app.Demos.web.Author;
import java.util.HashMap;
import java.util.Objects;
public class TokenMap {
public HashMap<String, Objects> getTokenMap(String ...av){
return new HashMap<>(){
private static final long serverid = 1L;
{
}
};
}
}

4
app-service/pom.xml

@ -42,6 +42,10 @@ @@ -42,6 +42,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
</dependencies>

38
app-service/src/main/java/co/depsystem/app/service/author/Impl/AuthorFilterImpl.java

@ -8,35 +8,57 @@ @@ -8,35 +8,57 @@
package co.depsystem.app.service.Author.Impl;
import co.depsystem.app.api.Common.ResponseResult;
import co.depsystem.app.api.WebUtil.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.io.IOException;
import java.util.HashMap;
/**
* 认证服务
* @author adiao
*/
@Service
public class AuthorFilterImpl implements AuthenticationEntryPoint {
public class AuthorFilterImpl extends BasicAuthenticationFilter {
@Resource
public ResponseResult<Object> result;
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
public AuthorFilterImpl(AuthenticationManager authenticationManager) {
super(authenticationManager);
}
/** 用户认证
* @param user User
* @return result;
*/
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
String token = request.getHeader("token");
if (ObjectUtils.isEmpty(token)){
chain.doFilter(request,response);
}
}
private UsernamePasswordAuthenticationFilter getAuthentication(String token){
HashMap<String,Object> userinfo = new HashMap<>();
JwtUtil.validateToken(token);
if (ObjectUtils.isEmpty(userinfo)){
return null;
}
String username =(String)userinfo.get("name");
String[] roles = (String[])userinfo.get("name");
return new UsernamePasswordAuthenticationFilter();
}
}

81
app-service/src/main/java/co/depsystem/app/service/author/Impl/LoginFilterImpl.java

@ -0,0 +1,81 @@ @@ -0,0 +1,81 @@
/**
* @author JOJO
* @class LoginFilterImpl
* @date 2023/4/12
* @apiNote
*/
package co.depsystem.app.service.Author.Impl;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationServiceException;
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 java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class LoginFilterImpl extends UsernamePasswordAuthenticationFilter {
/**
* @param request resq
* @param response resp
* @return Authentication
* @throws AuthenticationException error
*/
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
if (!request.getMethod().equals("POST")){
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
}
// 判断是否为JSON
if(request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE))
{
Map loginData = new HashMap<>();
try
{
//将Json数据转换为Map
loginData = JSONObject.parseObject(request.getInputStream().toString(), Map.class);
}catch (IOException e)
{
throw new RuntimeException(e);
}
String username = loginData.get(getUsernameParameter()).toString();
String password = loginData.get(getPasswordParameter()).toString();
if (username == null)
{
throw new AuthenticationServiceException("用户名不能为空");
}
if (password == null)
{
throw new AuthenticationServiceException("密码不能为空");
}
UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password);
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}else
{
request.getContentType();
//普通表单提交数据处理
String username = this.obtainUsername(request);
String password = this.obtainPassword(request);
if (username == null)
{
username = "";
}
if (password == null)
{
password = "";
}
username = username.trim();
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username,password);
this.setDetails(request,authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
}
}

6
pom.xml

@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<java.version>17</java.version>
</properties>
<modules>
@ -65,8 +65,8 @@ @@ -65,8 +65,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

4
start/src/main/java/co/depsystem/app/AppApplication.java

@ -2,7 +2,6 @@ package co.depsystem.app; @@ -2,7 +2,6 @@ package co.depsystem.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author adiao
@ -11,8 +10,7 @@ import org.springframework.context.ConfigurableApplicationContext; @@ -11,8 +10,7 @@ import org.springframework.context.ConfigurableApplicationContext;
public class AppApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(AppApplication.class, args);
System.out.println(run);
SpringApplication.run(AppApplication.class, args);
}
}

4
start/src/test/java/co/depsystem/app/AppApplicationTests.java

@ -8,6 +8,8 @@ class AppApplicationTests { @@ -8,6 +8,8 @@ class AppApplicationTests {
@Test
void contextLoads() {
}
}
}
Loading…
Cancel
Save