ソースを参照

[update] message:1.配置satoken的路由鉴权

kelei 6 ヶ月 前
コミット
8ce6b318ec
21 ファイル変更63 行追加43 行削除
  1. 14 1
      admin/src/main/java/com/flyer/foster/config/SaTokenConfigure.java
  2. 1 2
      admin/src/main/java/com/flyer/foster/controller/AreaController.java
  3. 1 2
      admin/src/main/java/com/flyer/foster/controller/FosterRecordController.java
  4. 1 2
      admin/src/main/java/com/flyer/foster/controller/GardenController.java
  5. 1 2
      admin/src/main/java/com/flyer/foster/controller/MenuController.java
  6. 1 2
      admin/src/main/java/com/flyer/foster/controller/OSSController.java
  7. 1 2
      admin/src/main/java/com/flyer/foster/controller/PosterLibController.java
  8. 1 2
      admin/src/main/java/com/flyer/foster/controller/RoleController.java
  9. 1 2
      admin/src/main/java/com/flyer/foster/controller/RoleMenuController.java
  10. 1 2
      admin/src/main/java/com/flyer/foster/controller/TenantController.java
  11. 1 2
      admin/src/main/java/com/flyer/foster/controller/TreeController.java
  12. 1 2
      admin/src/main/java/com/flyer/foster/controller/TreeImageController.java
  13. 1 2
      admin/src/main/java/com/flyer/foster/controller/TreePosterContentController.java
  14. 6 4
      admin/src/main/java/com/flyer/foster/controller/UserController.java
  15. 1 2
      admin/src/main/java/com/flyer/foster/controller/UserRoleController.java
  16. 1 2
      admin/src/main/java/com/flyer/foster/controller/WhisperController.java
  17. 4 5
      admin/src/main/java/com/flyer/foster/controller/app/AppUserController.java
  18. 19 0
      admin/src/main/java/com/flyer/foster/converter/WxMappingJackson2HttpMessageConverter.java
  19. 1 1
      admin/src/main/java/com/flyer/foster/service/IAppUserService.java
  20. 3 3
      admin/src/main/java/com/flyer/foster/service/impl/AppUserServiceImpl.java
  21. 2 1
      admin/src/main/java/com/flyer/foster/util/WeChatApiUtil.java

+ 14 - 1
admin/src/main/java/com/flyer/foster/config/SaTokenConfigure.java

@@ -1,6 +1,9 @@
 package com.flyer.foster.config;
 
 import cn.dev33.satoken.interceptor.SaInterceptor;
+import cn.dev33.satoken.router.SaRouter;
+import cn.dev33.satoken.stp.StpUtil;
+import com.flyer.foster.pojo.StpAppUtil;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -17,6 +20,16 @@ public class SaTokenConfigure implements WebMvcConfigurer {
     @Override
     public void addInterceptors(InterceptorRegistry registry) {
         // 注册 Sa-Token 拦截器,打开注解式鉴权功能
-        registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
+        registry.addInterceptor(new SaInterceptor(handler -> {
+            SaRouter.match("/admin/**")
+                    .notMatch("/admin/user/login", "/admin/user/generate-key")
+                    .check(r -> StpUtil.checkLogin());
+
+            SaRouter.match("/app/**")
+                    .notMatch("/app/user/login")
+                    .check(r -> StpAppUtil.checkLogin());
+
+
+        })).addPathPatterns("/**");
     }
 }

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/AreaController.java

@@ -23,9 +23,8 @@ import java.util.List;
  * @author flyer
  * @since 2024-05-09
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/area")
+@RequestMapping("/admin/area")
 public class AreaController {
     @Autowired
     private IAreaService iAreaService;

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/FosterRecordController.java

@@ -10,9 +10,8 @@ import org.springframework.web.bind.annotation.RestController;
  * @author flyer
  * @since 2024-05-09
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/foster-record")
+@RequestMapping("/admin/foster-record")
 public class FosterRecordController {
 
 }

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/GardenController.java

@@ -19,9 +19,8 @@ import java.util.List;
  * @author flyer
  * @since 2024-05-09
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/garden")
+@RequestMapping("/admin/garden")
 public class GardenController {
     @Autowired
     private IGardenService iGardenService;

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/MenuController.java

@@ -16,9 +16,8 @@ import org.springframework.web.bind.annotation.RestController;
  * @author flyer
  * @since 2024-05-06
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/menu")
+@RequestMapping("/admin/menu")
 public class MenuController {
     @Autowired
     private IMenuService iMenuService;

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/OSSController.java

@@ -20,9 +20,8 @@ import org.springframework.web.bind.annotation.RestController;
  * @since 2024/5/9/15:06
  */
 @Slf4j
-@SaCheckLogin
 @RestController
-@RequestMapping("/oss")
+@RequestMapping("/admin/oss")
 public class OSSController {
     private static String endpoint = "http://oss-cn-guangzhou.aliyuncs.com";
     private static String accessKeyId = "LTAI5t78gep5D1RCqvAEx8eJ";

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/PosterLibController.java

@@ -21,9 +21,8 @@ import java.util.List;
  * @author flyer
  * @since 2024-05-09
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/poster-lib")
+@RequestMapping("/admin/poster-lib")
 public class PosterLibController {
     @Autowired
     private IPosterLibService iPosterLibService;

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/RoleController.java

@@ -17,9 +17,8 @@ import java.util.List;
  * @author flyer
  * @since 2024-05-06
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/role")
+@RequestMapping("/admin/role")
 public class RoleController {
     @Autowired
     private IRoleService iRoleService;

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/RoleMenuController.java

@@ -15,9 +15,8 @@ import org.springframework.web.bind.annotation.RestController;
  * @author flyer
  * @since 2024-05-06
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/role-menu")
+@RequestMapping("/admin/role-menu")
 public class RoleMenuController {
     @Autowired
     private IRoleMenuService iRoleMenuService;

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/TenantController.java

@@ -10,9 +10,8 @@ import org.springframework.web.bind.annotation.RestController;
  * @author flyer
  * @since 2024-05-06
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/tenant")
+@RequestMapping("/admin/tenant")
 public class TenantController {
 
 }

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/TreeController.java

@@ -22,9 +22,8 @@ import java.util.List;
  * @author flyer
  * @since 2024-05-09
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/tree")
+@RequestMapping("/admin/tree")
 public class TreeController {
     @Autowired
     private ITreeService iTreeService;

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/TreeImageController.java

@@ -10,9 +10,8 @@ import org.springframework.web.bind.annotation.RestController;
  * @author flyer
  * @since 2024-05-09
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/tree-image")
+@RequestMapping("/admin/tree-image")
 public class TreeImageController {
 
 }

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/TreePosterContentController.java

@@ -10,9 +10,8 @@ import org.springframework.web.bind.annotation.RestController;
  * @author flyer
  * @since 2024-05-09
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/tree-poster-content")
+@RequestMapping("/admin/tree-poster-content")
 public class TreePosterContentController {
 
 }

+ 6 - 4
admin/src/main/java/com/flyer/foster/controller/UserController.java

@@ -27,9 +27,8 @@ import java.util.List;
  * @since 2024-05-06
  */
 @Slf4j
-@SaCheckLogin
 @RestController
-@RequestMapping("/user")
+@RequestMapping("/admin/user")
 public class UserController {
     @Autowired
     private IUserService iUserService;
@@ -40,13 +39,16 @@ public class UserController {
      * @param loginDto
      * @return
      */
-    @SaIgnore
     @PostMapping("/login")
     public R login(@RequestBody LoginDTO loginDto) {
         return R.ok().message("登录成功").result(iUserService.login(loginDto));
     }
 
-    @SaIgnore
+    /**
+     * 获取rsa公钥
+     * @param username
+     * @return
+     */
     @GetMapping("/generate-key")
     public R getRsaKey(String username) {
         return R.ok().result(iUserService.getRsaKey(username));

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/UserRoleController.java

@@ -10,9 +10,8 @@ import org.springframework.web.bind.annotation.RestController;
  * @author flyer
  * @since 2024-05-06
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/user-role")
+@RequestMapping("/admin/user-role")
 public class UserRoleController {
 
 }

+ 1 - 2
admin/src/main/java/com/flyer/foster/controller/WhisperController.java

@@ -22,9 +22,8 @@ import java.util.List;
  * @author flyer
  * @since 2024-05-09
  */
-@SaCheckLogin
 @RestController
-@RequestMapping("/whisper")
+@RequestMapping("/admin/whisper")
 public class WhisperController {
     @Autowired
     private IWhisperService iWhisperService;

+ 4 - 5
admin/src/main/java/com/flyer/foster/controller/app/AppUserController.java

@@ -1,13 +1,13 @@
 package com.flyer.foster.controller.app;
 
 import cn.dev33.satoken.annotation.SaCheckLogin;
+import cn.dev33.satoken.annotation.SaIgnore;
 import com.flyer.foster.dto.AppUserQueryDTO;
 import com.flyer.foster.pojo.StpAppUtil;
 import com.flyer.foster.service.IAppUserService;
 import com.flyer.util.R;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestHeader;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -17,15 +17,14 @@ import org.springframework.web.bind.annotation.RestController;
  * @author flyer
  * @since 2024-05-09
  */
-@SaCheckLogin(type = StpAppUtil.TYPE)
 @RestController
-@RequestMapping("/app-user")
+@RequestMapping("/app/user")
 public class AppUserController {
     @Autowired
     private IAppUserService iAppUserService;
 
     @RequestMapping("/login")
-    public R login(@RequestBody AppUserQueryDTO dto, @RequestHeader("appId") String appId) {
-        return R.ok().result(iAppUserService.login(dto, appId));
+    public R login(@RequestBody AppUserQueryDTO dto) {
+        return R.ok().result(iAppUserService.login(dto));
     }
 }

+ 19 - 0
admin/src/main/java/com/flyer/foster/converter/WxMappingJackson2HttpMessageConverter.java

@@ -0,0 +1,19 @@
+package com.flyer.foster.converter;
+
+import org.springframework.http.MediaType;
+import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
+
+import java.util.ArrayList;
+
+/**
+ * 解决调用微信服务返回JSON,同时返回携带的 Header 里面的 Content-Type 值 text/plain,无法反序列化问题
+ *
+ */
+public class WxMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
+
+    public WxMappingJackson2HttpMessageConverter(){
+        ArrayList<MediaType> mediaTypes = new ArrayList<>();
+        mediaTypes.add(MediaType.TEXT_PLAIN);
+        setSupportedMediaTypes(mediaTypes);
+    }
+}

+ 1 - 1
admin/src/main/java/com/flyer/foster/service/IAppUserService.java

@@ -15,5 +15,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface IAppUserService extends IService<AppUser> {
 
-    AppUserRespDTO login(AppUserQueryDTO dto, String appId);
+    AppUserRespDTO login(AppUserQueryDTO dto);
 }

+ 3 - 3
admin/src/main/java/com/flyer/foster/service/impl/AppUserServiceImpl.java

@@ -44,9 +44,9 @@ public class AppUserServiceImpl extends ServiceImpl<IAppUserMapper, AppUser> imp
     private IAppUserService iAppUserService;
 
     @Override
-    public AppUserRespDTO login(AppUserQueryDTO dto, String appId) {
+    public AppUserRespDTO login(AppUserQueryDTO dto) {
         // 查找用户信息
-        Code2SessionResp code2Session = weChatApiUtil.getCode2Session(dto.getCode(), appId, WechatConst.APP_SECRET);
+        Code2SessionResp code2Session = weChatApiUtil.getCode2Session(dto.getCode(), WechatConst.APP_ID, WechatConst.APP_SECRET);
         String openid = code2Session.getOpenid();
         // 查找用户信息,存在更新,不存在新增
         AppUser appUser = iAppUserService.lambdaQuery().eq(AppUser::getOpenId, openid).one();
@@ -61,7 +61,7 @@ public class AppUserServiceImpl extends ServiceImpl<IAppUserMapper, AppUser> imp
         }
         AppUserRespDTO respDTO = new AppUserRespDTO();
         BeanUtil.copyProperties(appUser, respDTO);
-        StpAppUtil.login(respDTO, LoginDevice.APP);
+        StpAppUtil.login(respDTO.getOpenId(), LoginDevice.APP);
         return respDTO;
     }
 }

+ 2 - 1
admin/src/main/java/com/flyer/foster/util/WeChatApiUtil.java

@@ -3,6 +3,7 @@ package com.flyer.foster.util;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.flyer.exception.BusinessException;
+import com.flyer.foster.converter.WxMappingJackson2HttpMessageConverter;
 import com.flyer.foster.pojo.Code2SessionResp;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -29,7 +30,7 @@ public class WeChatApiUtil {
     @PostConstruct
     public void initRestTemplate() {
         // 解决调用微信服务返回信息无法反序列化
-//        restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
+        restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
     }
 
     /**