ソースを参照

添加swagger

785834757 1 年間 前
コミット
bd56e8eab3

+ 4 - 3
authentication/src/main/java/com/jzg/config/SecurityConfig.java

@@ -51,14 +51,15 @@ public class SecurityConfig {
     public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
 
         http.authorizeHttpRequests(auth -> auth
-                        .requestMatchers("**").permitAll()  // 公开访问
+                        .requestMatchers("/swagger-ui/**", "/v3/api-docs/**", "/swagger-resources/**", "/webjars/**", "/favicon.ico").permitAll()
                         .requestMatchers("/auth/login").permitAll()
                         .requestMatchers("/auth/getToken").permitAll()
-                        .requestMatchers("/auth/*").permitAll() // 公开访问
-                        .requestMatchers("/auth1/getUser").permitAll()  // 公开访问
+                        .requestMatchers("/auth/*").permitAll()
+                        .requestMatchers("/auth1/getUser").permitAll()
                         .requestMatchers("favicon.ico").permitAll()
                         .anyRequest().authenticated()  // 其他接口需认证
                 )
+//                .formLogin().disable()
                 .formLogin((form) -> form
                         .loginPage("/login")
                         .successHandler(new JzgAuthenticationSuccessHandler(tokenService))

+ 16 - 8
authentication/src/main/java/com/jzg/controller/AuthController.java

@@ -6,8 +6,12 @@ import com.jzg.config.JzgTokenService;
 import com.jzg.config.JzgUserDetailsService;
 import com.jzg.config.JzgUsernamePasswordAuthenticationToken;
 import com.jzg.entity.SysUser;
+import com.jzg.entity.vo.LoginFormVo;
 import com.jzg.handler.JzgAuthenticationSuccessHandler;
 import com.jzg.service.SysUserService;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.responses.ApiResponse;
+import io.swagger.v3.oas.models.annotations.OpenAPI30;
 import jakarta.servlet.ServletException;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
@@ -15,9 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.authentication.AuthenticationManager;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
 
@@ -41,16 +43,22 @@ public class AuthController {
 
     /**
      * 登录接口
-     * @param userName
-     * @param passWord
+     * @param loginFormVo
      * @throws ServletException
      * @throws IOException
      */
-    @GetMapping("/login")
-    public void login(String userName,String passWord,String system) throws ServletException, IOException {
+    @Operation(
+            summary = "登录接口",
+            description = "登录接口",
+    responses = {
+            @ApiResponse(responseCode = "200",description = "登录成功"),
+            @ApiResponse(responseCode = "403",description = "登录失败")
+    })
+    @PostMapping("/login")
+    public void login(@RequestBody LoginFormVo loginFormVo) throws ServletException, IOException {
         HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
         HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
-        JzgUsernamePasswordAuthenticationToken authentication = new JzgUsernamePasswordAuthenticationToken(userName,passWord,system);
+        JzgUsernamePasswordAuthenticationToken authentication = new JzgUsernamePasswordAuthenticationToken(loginFormVo.getUserName(),loginFormVo.getPassWord(),loginFormVo.getSystem());
         Authentication authenticate = authenticationManager.authenticate(authentication);
         SecurityContextHolder.getContext().setAuthentication(authenticate);
         JzgAuthenticationSuccessHandler successHandler = new JzgAuthenticationSuccessHandler(tokenStore);

+ 21 - 0
authentication/src/main/java/com/jzg/entity/vo/LoginFormVo.java

@@ -0,0 +1,21 @@
+package com.jzg.entity.vo;
+
+//import io.swagger.annotations.ApiModel;
+//import io.swagger.annotations.ApiModelProperty;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+@Data
+@Schema(description = "登录表单VO")
+public class LoginFormVo {
+
+    @Schema(description = "账号")
+    private String userName;
+
+    @Schema(description = "密码")
+    private String passWord;
+
+    @Schema(description = "系统code")
+    private String system;
+
+}

+ 7 - 0
authentication/src/main/resources/application.yml

@@ -16,6 +16,13 @@ spring:
 mybatis-plus:
   mapper-locations: classpath*:/mapper/*Mapper.xml
 
+logging:
+  charset:
+    console: UTF-8
+  config: classpath:logback.xml
+  file:
+    path: logs/logs.log
+
 
 config:
   sso: 1

+ 6 - 0
commons/pom.xml

@@ -16,5 +16,11 @@
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
         </dependency>
+
+        <!--swagger -->
+        <dependency>
+            <groupId>org.springdoc</groupId>
+            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 1 - 1
commons/src/main/java/com/jzg/config/RedissonConfig.java

@@ -21,7 +21,7 @@ public class RedissonConfig {
         List<String> redisAddress = new ArrayList<>();
 //        redisAddress.add("redis://192.168.0.250:6379");
         SingleServerConfig clusterConfig = config.useSingleServer()
-                .setAddress("redis://192.168.0.250:6379")
+                .setAddress("redis://192.168.5.250:6379")
                 .setPassword("123456")
                 .setTimeout(3000);
                   // 从连接池大小

+ 18 - 0
commons/src/main/java/com/jzg/config/SwaggerConfig.java

@@ -0,0 +1,18 @@
+package com.jzg.config;
+
+import io.swagger.v3.oas.models.OpenAPI;
+import io.swagger.v3.oas.models.info.Info;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class SwaggerConfig {
+    @Bean
+    public OpenAPI customOpenAPI() {
+        return new OpenAPI()
+                .info(new Info()
+                        .title("API Documentation")
+                        .version("1.0")
+                        .description("Spring Boot 3.x API documentation using springdoc-openapi"));
+    }
+}

+ 20 - 0
pom.xml

@@ -70,16 +70,36 @@
       <artifactId>fastjson</artifactId>
       <version>${fastjson.version}</version>
     </dependency>
+
   </dependencies>
   <dependencyManagement>
     <dependencies>
 
+      <!--swagger -->
+      <dependency>
+        <groupId>org.springdoc</groupId>
+        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
+        <version>2.1.0</version>
+      </dependency>
+
       <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-security</artifactId>
         <version>3.0.0</version>
       </dependency>
 
+      <dependency>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-webflux</artifactId>
+      </dependency>
+
+      <!-- 状态机 -->
+      <dependency>
+        <groupId>org.springframework.statemachine</groupId>
+        <artifactId>spring-statemachine-core</artifactId>
+        <version>3.1.0</version> <!-- 你可以选择适合你的版本 -->
+      </dependency>
+
       <!-- 数据库驱动,如 MySQL -->
       <dependency>
         <groupId>mysql</groupId>