diff --git a/src/main/java/com/rymcu/forest/auth/JwtFilter.java b/src/main/java/com/rymcu/forest/auth/JwtFilter.java index f184811..37fe085 100644 --- a/src/main/java/com/rymcu/forest/auth/JwtFilter.java +++ b/src/main/java/com/rymcu/forest/auth/JwtFilter.java @@ -1,5 +1,7 @@ package com.rymcu.forest.auth; +import com.alibaba.fastjson2.JSONObject; +import com.rymcu.forest.core.result.GlobalResultGenerator; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureException; @@ -72,14 +74,20 @@ public class JwtFilter extends BasicHttpAuthenticationFilter { */ @Override protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) { + return false; + } + + @Override + protected boolean onAccessDenied(ServletRequest request, ServletResponse response) { if (isLoginAttempt(request, response)) { try { executeLogin(request, response); } catch (Exception e) { - response401(request, response); + onLoginFail(response); } } - return true; + onLoginFail(response); + return false; } /** @@ -104,12 +112,12 @@ public class JwtFilter extends BasicHttpAuthenticationFilter { /** * 将非法请求跳转到 /401 */ - private void response401(ServletRequest request, ServletResponse response) { + private void onLoginFail(ServletResponse response) { try { - HttpServletResponse httpResponse = (HttpServletResponse) response; - httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED); - httpResponse.setContentType("application/json;charset=utf-8"); - httpResponse.getOutputStream().write("login fail".getBytes()); + HttpServletResponse httpServletResponse = (HttpServletResponse) response; + httpServletResponse.setContentType("application/json"); + httpServletResponse.setCharacterEncoding("UTF-8"); + httpServletResponse.getOutputStream().write(JSONObject.toJSONString(GlobalResultGenerator.genErrorResult("未登录或已登录超时,请重新登录")).getBytes()); } catch (IOException e) { // 错误日志 log.error(e.getMessage()); diff --git a/src/main/java/com/rymcu/forest/config/ShiroConfig.java b/src/main/java/com/rymcu/forest/config/ShiroConfig.java index 2c78f14..bdab6d4 100644 --- a/src/main/java/com/rymcu/forest/config/ShiroConfig.java +++ b/src/main/java/com/rymcu/forest/config/ShiroConfig.java @@ -34,6 +34,12 @@ public class ShiroConfig { Map filterChainDefinitionMap = new LinkedHashMap(); filterChainDefinitionMap.put("/api/v1/console/**", "anon"); + filterChainDefinitionMap.put("/api/v1/article/detail/**", "anon"); + filterChainDefinitionMap.put("/api/v1/topic/**", "anon"); + filterChainDefinitionMap.put("/api/v1/user/**", "anon"); + filterChainDefinitionMap.put("/api/v1/article/*/comments", "anon"); + filterChainDefinitionMap.put("/api/v1/rule/currency/**", "anon"); + filterChainDefinitionMap.put("/api/v1/lucene/**", "anon"); filterChainDefinitionMap.put("/api/v1/open-data/**", "anon"); filterChainDefinitionMap.put("/**", "jwt"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); diff --git a/src/main/java/com/rymcu/forest/web/api/user/UserController.java b/src/main/java/com/rymcu/forest/web/api/user/UserController.java index 01756f1..c591b9b 100644 --- a/src/main/java/com/rymcu/forest/web/api/user/UserController.java +++ b/src/main/java/com/rymcu/forest/web/api/user/UserController.java @@ -36,15 +36,15 @@ public class UserController { @GetMapping("/{account}") @VisitLogger - public GlobalResult detail(@PathVariable String account){ + public GlobalResult detail(@PathVariable String account) { UserDTO userDTO = userService.findUserDTOByAccount(account); return GlobalResultGenerator.genSuccessResult(userDTO); } @GetMapping("/{account}/articles") - public GlobalResult> userArticles(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "12") Integer rows, @PathVariable String account){ + public GlobalResult> userArticles(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "12") Integer rows, @PathVariable String account) { UserDTO userDTO = userService.findUserDTOByAccount(account); - if (userDTO == null){ + if (userDTO == null) { return GlobalResultGenerator.genErrorResult("用户不存在!"); } PageHelper.startPage(page, rows); @@ -54,9 +54,9 @@ public class UserController { } @GetMapping("/{account}/portfolios") - public GlobalResult> userPortfolios(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "12") Integer rows, @PathVariable String account){ + public GlobalResult> userPortfolios(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "12") Integer rows, @PathVariable String account) { UserDTO userDTO = userService.findUserDTOByAccount(account); - if (userDTO == null){ + if (userDTO == null) { return GlobalResultGenerator.genErrorResult("用户不存在!"); } PageHelper.startPage(page, rows); @@ -66,9 +66,9 @@ public class UserController { } @GetMapping("/{account}/followers") - public GlobalResult> userFollowers(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "12") Integer rows, @PathVariable String account){ + public GlobalResult> userFollowers(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "12") Integer rows, @PathVariable String account) { UserDTO userDTO = userService.findUserDTOByAccount(account); - if (userDTO == null){ + if (userDTO == null) { return GlobalResultGenerator.genErrorResult("用户不存在!"); } PageHelper.startPage(page, rows); @@ -78,9 +78,9 @@ public class UserController { } @GetMapping("/{account}/followings") - public GlobalResult> userFollowings(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "12") Integer rows, @PathVariable String account){ + public GlobalResult> userFollowings(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "12") Integer rows, @PathVariable String account) { UserDTO userDTO = userService.findUserDTOByAccount(account); - if (userDTO == null){ + if (userDTO == null) { return GlobalResultGenerator.genErrorResult("用户不存在!"); } PageHelper.startPage(page, rows);