From 426c1b29fa2c427bb036f08b0147a8e46a538adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=96=87?= <289222346@qq.com> Date: Wed, 9 Nov 2022 11:48:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor(=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81):=20=E5=B0=86=E9=83=A8=E5=88=86=E7=BB=8F=E5=B8=B8?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=E5=8C=96=EF=BC=8C=E5=88=A9=E4=BA=8E=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/boss/portal/config/SwaggerConfig.java | 8 +++++--- src/main/java/boss/portal/constant/ConstantKey.java | 12 +++++++++++- .../boss/portal/filter/JWTAuthenticationFilter.java | 10 +++++----- src/main/java/boss/portal/filter/JWTLoginFilter.java | 6 +++--- .../handler/Http401AuthenticationEntryPoint.java | 6 ++++-- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/main/java/boss/portal/config/SwaggerConfig.java b/src/main/java/boss/portal/config/SwaggerConfig.java index 5087ca9..cea9bac 100644 --- a/src/main/java/boss/portal/config/SwaggerConfig.java +++ b/src/main/java/boss/portal/config/SwaggerConfig.java @@ -3,6 +3,8 @@ package boss.portal.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMethod; + +import boss.portal.constant.ConstantKey; import springfox.documentation.builders.ParameterBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; @@ -31,13 +33,13 @@ import static cn.hutool.core.collection.CollUtil.newArrayList; public class SwaggerConfig { // 设置默认TOKEN,方便测试 - private static final String TOKEN = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ6aGFveGluZ3VvLVtST0xFX0FETUlOLCBBVVRIX1dSSVRFXSIsImV4cCI6MTUzOTMzOTM0NX0.P9dkLQ7lpNODJppHBM-InSS90nw0XJieK8QNlZM0TeuNNQ8sUPYH-uif099A1-P2Ap6b_9lCLbXL2iR0OLdFyw"; + private static final String TOKEN = ConstantKey.BEARER + "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ6aGFveGluZ3VvLVtST0xFX0FETUlOLCBBVVRIX1dSSVRFXSIsImV4cCI6MTUzOTMzOTM0NX0.P9dkLQ7lpNODJppHBM-InSS90nw0XJieK8QNlZM0TeuNNQ8sUPYH-uif099A1-P2Ap6b_9lCLbXL2iR0OLdFyw"; @Bean public Docket api() { ParameterBuilder tokenPar = new ParameterBuilder(); List pars = new ArrayList(); - tokenPar.name("Authorization").description("令牌").defaultValue(TOKEN).modelRef(new ModelRef("string")).parameterType("header").required(false).build(); + tokenPar.name(ConstantKey.HEADER_KEY).description("令牌").defaultValue(TOKEN).modelRef(new ModelRef("string")).parameterType("header").required(false).build(); pars.add(tokenPar.build()); Docket docket = new Docket(DocumentationType.SWAGGER_2) .select() @@ -60,4 +62,4 @@ public class SwaggerConfig { new Contact("Zhao XinGuo", "https://javaymw.com/", "sxdtzhaoxinguo@163.com"), "License of API", "API license URL", Collections.emptyList()); } -} +} \ No newline at end of file diff --git a/src/main/java/boss/portal/constant/ConstantKey.java b/src/main/java/boss/portal/constant/ConstantKey.java index d98d933..7227a96 100644 --- a/src/main/java/boss/portal/constant/ConstantKey.java +++ b/src/main/java/boss/portal/constant/ConstantKey.java @@ -13,4 +13,14 @@ public class ConstantKey { * 签名key */ public static final String SIGNING_KEY = "spring-security-@Jwt!&Secret^#"; -} + + /** + * 持票人 + */ + public static final String BEARER = "Bearer "; + + /** + * 在头部标签中存放Token的key + */ + public static final String HEADER_KEY = "Authorization"; +} \ No newline at end of file diff --git a/src/main/java/boss/portal/filter/JWTAuthenticationFilter.java b/src/main/java/boss/portal/filter/JWTAuthenticationFilter.java index 729844e..ca4cf9b 100644 --- a/src/main/java/boss/portal/filter/JWTAuthenticationFilter.java +++ b/src/main/java/boss/portal/filter/JWTAuthenticationFilter.java @@ -39,8 +39,8 @@ public class JWTAuthenticationFilter extends BasicAuthenticationFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { - String header = request.getHeader("Authorization"); - if (ObjectUtil.isEmpty(header) || !header.startsWith("Bearer ")) { + String header = request.getHeader(ConstantKey.HEADER_KEY); + if (ObjectUtil.isEmpty(header) || !header.startsWith(ConstantKey.BEARER)) { chain.doFilter(request, response); return; } @@ -52,14 +52,14 @@ public class JWTAuthenticationFilter extends BasicAuthenticationFilter { private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { long start = System.currentTimeMillis(); - String token = request.getHeader("Authorization"); + String token = request.getHeader(ConstantKey.HEADER_KEY); if (ObjectUtil.isEmpty(token)) { throw new ServiceException("Token不能为空!"); } // parse the token. String user = null; - Claims claims = Jwts.parser().setSigningKey(ConstantKey.SIGNING_KEY).parseClaimsJws(token.replace("Bearer ", "")).getBody(); + Claims claims = Jwts.parser().setSigningKey(ConstantKey.SIGNING_KEY).parseClaimsJws(token.replace(ConstantKey.BEARER, "")).getBody(); // token签发时间 long issuedAt = claims.getIssuedAt().getTime(); // 当前时间 @@ -131,4 +131,4 @@ public class JWTAuthenticationFilter extends BasicAuthenticationFilter { return null; } -} +} \ No newline at end of file diff --git a/src/main/java/boss/portal/filter/JWTLoginFilter.java b/src/main/java/boss/portal/filter/JWTLoginFilter.java index dc7850d..fdc2f87 100644 --- a/src/main/java/boss/portal/filter/JWTLoginFilter.java +++ b/src/main/java/boss/portal/filter/JWTLoginFilter.java @@ -100,11 +100,11 @@ public class JWTLoginFilter extends UsernamePasswordAuthenticationFilter { // 生成token end // 登录成功后,返回token到header里面 - /*response.addHeader("Authorization", "Bearer " + token);*/ + /*response.addHeader(ConstantKey.HEADER_KEY, ConstantKey.BEARER + token);*/ // 登录成功后,返回token到body里面 Map resultMap = new HashMap<>(); - resultMap.put("Authorization", "Bearer " + token); + resultMap.put(ConstantKey.HEADER_KEY, ConstantKey.BEARER + token); Result result = Result.ok(resultMap); response.getWriter().write(JSON.toJSONString(result)); @@ -113,4 +113,4 @@ public class JWTLoginFilter extends UsernamePasswordAuthenticationFilter { } } -} +} \ No newline at end of file diff --git a/src/main/java/boss/portal/handler/Http401AuthenticationEntryPoint.java b/src/main/java/boss/portal/handler/Http401AuthenticationEntryPoint.java index e3587a2..0716453 100644 --- a/src/main/java/boss/portal/handler/Http401AuthenticationEntryPoint.java +++ b/src/main/java/boss/portal/handler/Http401AuthenticationEntryPoint.java @@ -8,6 +8,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import boss.portal.constant.ConstantKey; + /** * @Auther: zhaoxinguo * @Date: 2018/9/20 14:55 @@ -23,8 +25,8 @@ public class Http401AuthenticationEntryPoint implements AuthenticationEntryPoint @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { - response.setHeader("Authorization", this.headerValue); + response.setHeader(ConstantKey.HEADER_KEY, this.headerValue); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage()); } -} +} \ No newline at end of file -- Gitee From dec717fd0f04f09b30d2a6e099cb70e52d2d0375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=96=87?= <289222346@qq.com> Date: Wed, 9 Nov 2022 12:10:09 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(=E5=BC=82=E5=B8=B8=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E9=94=99=E8=AF=AF):Jwt=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=B1=BB=EF=BC=8C=E7=AD=BE=E5=90=8D=E5=BC=82=E5=B8=B8=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E9=94=99=E8=AF=AF=E7=9A=84=E4=BD=BF=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E8=BF=87=E6=9C=9F=E5=BC=82=E5=B8=B8=EF=BC=8C=E5=B7=B2=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/boss/portal/controller/JwtExceptionController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/boss/portal/controller/JwtExceptionController.java b/src/main/java/boss/portal/controller/JwtExceptionController.java index 661b6f7..846c992 100644 --- a/src/main/java/boss/portal/controller/JwtExceptionController.java +++ b/src/main/java/boss/portal/controller/JwtExceptionController.java @@ -34,7 +34,7 @@ public class JwtExceptionController { @RequestMapping("/signatureException") public void signatureException(HttpServletRequest request) throws SignatureException { - if (request.getAttribute("signatureException") instanceof ExpiredJwtException) { + if (request.getAttribute("signatureException") instanceof SignatureException) { throw ((SignatureException) request.getAttribute("signatureException")); } } -- Gitee