Skip to content

Commit

Permalink
1、优化了 jwt 过滤器判断(删除了一些无效判断)
Browse files Browse the repository at this point in the history
2、后台添加 logo 图片
3、登录页美化
  • Loading branch information
weiwosuoai committed Sep 13, 2023
1 parent 02a13c3 commit a46799d
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 191 deletions.
21 changes: 12 additions & 9 deletions weblog-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${revision}</version>
<name>weblog-springboot</name>
<description>weblog-springboot</description>
<description>前后端分离博客 Weblog By 犬小哈</description>

<!-- 版本号统一管理 -->
<properties>
<revision>0.0.1-SNAPSHOT</revision>
<java.version>1.8</java.version>
<!-- 依赖包版本 -->
<mapstruct.version>1.5.5.Final</mapstruct.version>
Expand All @@ -29,8 +30,12 @@
<minio.version>8.2.1</minio.version>
<flexmark.version>0.62.2</flexmark.version>

<!-- 插件版本 -->
<!-- Maven 相关 -->
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- 多模块项目父工程必须指定为 pom -->
Expand All @@ -50,25 +55,25 @@
<dependency>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-module-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-module-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-module-jwt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${revision}</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -164,8 +169,6 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source> <!-- depending on your project -->
<target>${java.version}</target> <!-- depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
Expand Down
3 changes: 1 addition & 2 deletions weblog-springboot/weblog-module-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
<parent>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-module-admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>weblog-module-admin</name>
<description>weblog-module-admin</description>

Expand Down
3 changes: 1 addition & 2 deletions weblog-springboot/weblog-module-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
<parent>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-module-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>weblog-module-common</name>
<description>weblog-module-common</description>

Expand Down
3 changes: 1 addition & 2 deletions weblog-springboot/weblog-module-jwt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
<parent>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${revision}</version>
</parent>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-module-jwt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>weblog-module-jwt</name>
<description>weblog-module-jwt</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
Expand All @@ -19,6 +20,7 @@
* @date: 2023-04-18 16:21
* @description: 登录认证过滤器
**/
@Slf4j
public class JwtAuthenticationLoginFilter extends AbstractAuthenticationProcessingFilter {

/**
Expand All @@ -35,6 +37,8 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
String username = jsonNode.get("username").textValue();
String password = jsonNode.get("password").textValue();

log.info("==> 用户尝试登录认证,username: {}, password: {}", username, password);

// 将用户名、密码封装到 Token 中
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken
= new UsernamePasswordAuthenticationToken(username, password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.BadCredentialsException;
Expand Down Expand Up @@ -63,7 +64,7 @@ public void afterPropertiesSet() {
///////////////////////////////////////////////////////////////////////////
public String generateToken(String username) {
LocalDateTime now = LocalDateTime.now();
// Token 一个小时内有效
// 设置 Token 失效时间
LocalDateTime expireTime = now.plusHours(tokenExpireTime);

return Jwts.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.common.enums.ResponseCodeEnum;
import com.quanxiaoha.weblog.jwt.util.ResultUtil;
import com.quanxiaoha.weblog.jwt.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.jwt.model.CreateTokenRspVO;
import com.quanxiaoha.weblog.jwt.util.ResultUtil;
import com.quanxiaoha.weblog.jwt.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -21,6 +22,7 @@
* @description: 登录认证成功后处理
**/
@Component
@Slf4j
public class LoginAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Autowired
private JwtTokenHelper jwtTokenHelper;
Expand All @@ -38,6 +40,8 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
.token(token)
.build();

log.info("==> 用户认证成功, username: {}, token: {}", username, token);

ResultUtil.ok(response, Response.success(tokenRspDTO));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.common.enums.ResponseCodeEnum;
import com.quanxiaoha.weblog.jwt.util.ResultUtil;
import com.quanxiaoha.weblog.jwt.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@

import com.quanxiaoha.weblog.common.Response;
import com.quanxiaoha.weblog.common.enums.ResponseCodeEnum;
import com.quanxiaoha.weblog.jwt.util.ResultUtil;
import com.quanxiaoha.weblog.jwt.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SignatureException;

/**
* @author: 犬小哈
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,17 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
if (StringUtils.isNotBlank(token) && jwtTokenHelper.validateToken(token)) {
String username = jwtTokenHelper.getUsernameByToken(token);

// 判断 token 是否有效
if (StringUtils.isNotBlank(username)
&& Objects.isNull(SecurityContextHolder.getContext().getAuthentication())) {
if (StringUtils.isNotBlank(username)) {
// && Objects.isNull(SecurityContextHolder.getContext().getAuthentication())

UserDetails userDetails = userDetailsService.loadUserByUsername(username);

boolean isTokenValid = jwtTokenHelper.isTokenValid(token, userDetails);

if (isTokenValid) {
// 将用户信息存入 authentication,方便后续校验
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null,
userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
// 将 authentication 存入 ThreadLocal,方便后续获取用户信息
SecurityContextHolder.getContext().setAuthentication(authentication);
}
// 将用户信息存入 authentication,方便后续校验
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null,
userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
// 将 authentication 存入 ThreadLocal,方便后续获取用户信息
SecurityContextHolder.getContext().setAuthentication(authentication);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.quanxiaoha.weblog.jwt.util;
package com.quanxiaoha.weblog.jwt.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.quanxiaoha.weblog.common.Response;
Expand Down
3 changes: 1 addition & 2 deletions weblog-springboot/weblog-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
<parent>
<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>${revision}</version>
</parent>

<groupId>com.quanxiaoha</groupId>
<artifactId>weblog-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>weblog-web</name>
<description>weblog-web</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jwt:


#=================================================================
# minio
# minio (上传图片需要,需配置成自己的地址)
#=================================================================
minio:
endpoint: https://img.xxx.com
Expand Down
Loading

0 comments on commit a46799d

Please sign in to comment.