diff --git a/pom.xml b/pom.xml index 0503bcb..92c9f8f 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,11 @@ spring-boot-starter-tomcat provided + + com.alibaba.fastjson2 + fastjson2 + 2.0.25 + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/com/example/RestBean.java b/src/main/java/com/example/RestBean.java new file mode 100644 index 0000000..ffe3062 --- /dev/null +++ b/src/main/java/com/example/RestBean.java @@ -0,0 +1,22 @@ +package com.example; + +import com.alibaba.fastjson2.JSONObject; +import com.alibaba.fastjson2.JSONWriter; + +public record RestBean(int code , T data , String message) { + public static RestBean success(T data){ + return new RestBean<>(200, data ,"请求成功"); + + } + public static RestBean success(){ + return success(null); + + } + + public static RestBean failure(int code , String message){ + return new RestBean<>(code , null , message); + } + public String asJsonString(){ + return JSONObject.toJSONString(this, JSONWriter.Feature.WriteNulls); + } +} diff --git a/src/main/java/com/example/config/SecurityConfiguration.java b/src/main/java/com/example/config/SecurityConfiguration.java new file mode 100644 index 0000000..edbdc45 --- /dev/null +++ b/src/main/java/com/example/config/SecurityConfiguration.java @@ -0,0 +1,69 @@ +package com.example.config; + +import com.example.RestBean; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.config.http.SessionCreationPolicy; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.SecurityFilterChain; +import org.springframework.security.web.authentication.AuthenticationFailureHandler; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; +import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; + +import java.io.IOException; + +@Configuration +public class SecurityConfiguration { + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + return http + .authorizeHttpRequests(conf -> conf + .requestMatchers("/api/auth/**").permitAll() + .anyRequest().authenticated() + ) + .formLogin(conf -> conf + .loginProcessingUrl("/api/auth/login") + .failureHandler(this::onAuthenticationFailure) + .successHandler(this::onAuthenticationSuccess) + + ) + .logout(conf -> conf + .logoutUrl("/api/auth/logout") + .logoutSuccessHandler(this::onLogoutSuccess) + ) + + .csrf(AbstractHttpConfigurer::disable) + .sessionManagement(conf -> conf + .sessionCreationPolicy(SessionCreationPolicy.STATELESS) + ) + .build(); + } + + public void onAuthenticationSuccess(HttpServletRequest request, + HttpServletResponse response, + Authentication authentication) throws IOException, ServletException { + response.setContentType("application/json;charset=utf-8"); + + response.getWriter().write(RestBean.success().asJsonString()); + } + + public void onAuthenticationFailure(HttpServletRequest request, + HttpServletResponse response, + AuthenticationException exception) throws IOException, ServletException { + response.setContentType("application/json;charset=utf-8"); + response.getWriter().write(RestBean.failure(401 , exception.getMessage()).asJsonString()); + + } + + public void onLogoutSuccess(HttpServletRequest request, + HttpServletResponse response, + Authentication authentication) throws IOException, ServletException { + + } +} \ No newline at end of file