From fd756fe7b359f88213d3ffaa20c94b2fe1eba62c Mon Sep 17 00:00:00 2001 From: lu <2066448475@qq.com> Date: Mon, 13 Jan 2025 22:24:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=AE=89=E5=85=A8=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/compiler.xml | 1 + .idea/dataSources.xml | 18 +++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ README.md | 7 + study-project-backend/pom.xml | 7 +- .../example/config/SecurityConfiguration.java | 52 ++++++++ .../java/com/example/entity/RestBean.java | 44 +++++++ .../src/main/resources/application.yml | 7 +- 8 files changed, 258 insertions(+), 2 deletions(-) create mode 100644 .idea/dataSources.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 README.md create mode 100644 study-project-backend/src/main/java/com/example/config/SecurityConfiguration.java create mode 100644 study-project-backend/src/main/java/com/example/entity/RestBean.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index defdc91..62869c0 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -2,6 +2,7 @@ + diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..73a6798 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,18 @@ + + + + + mysql.8 + true + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306/study + + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..cab9624 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +## 前后端分离项目模板 + +包含基本的登录、注册、密码重置等功能,可以二次开发编写具体场景下的应用程序。 + +* 登录功能(支持用户名、邮箱登录) +* 注册用户(通过邮箱注册) +* 重置密码(通过邮箱重置密码) \ No newline at end of file diff --git a/study-project-backend/pom.xml b/study-project-backend/pom.xml index 87efd16..ee98bb6 100644 --- a/study-project-backend/pom.xml +++ b/study-project-backend/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.4.1 + 3.0.5 com.example @@ -74,6 +74,11 @@ spring-security-test test + + com.alibaba.fastjson2 + fastjson2 + 2.0.51 + diff --git a/study-project-backend/src/main/java/com/example/config/SecurityConfiguration.java b/study-project-backend/src/main/java/com/example/config/SecurityConfiguration.java new file mode 100644 index 0000000..0c76c4e --- /dev/null +++ b/study-project-backend/src/main/java/com/example/config/SecurityConfiguration.java @@ -0,0 +1,52 @@ +package com.example.config; + +import com.alibaba.fastjson2.JSONObject; +import com.example.entity.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.configuration.EnableWebSecurity; +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 java.io.IOException; + +@Configuration +@EnableWebSecurity +public class SecurityConfiguration { + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + return http + .authorizeHttpRequests() + .anyRequest().authenticated() + .and() + .formLogin() + .loginProcessingUrl("/api/auth/login") + .successHandler(this::onAuthenticationSuccess) + .failureHandler(this::onAuthenticationFailure) + .and() + .logout() + .logoutUrl("api/auth/logout") + .and() + .csrf() + .disable() + .exceptionHandling() + .authenticationEntryPoint(this::onAuthenticationFailure) + .and() + .build(); + } + public void onAuthenticationSuccess (HttpServletRequest request, HttpServletResponse response, Authentication auth) throws IOException { + response.setCharacterEncoding("UTF-8"); + response.getWriter().write(JSONObject.toJSONString(RestBean.success("登录成功!"))); + } + + public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { + response.setCharacterEncoding("UTF-8"); + response.getWriter().write(JSONObject.toJSONString(RestBean.failure(401, exception.getMessage()))); + } +} diff --git a/study-project-backend/src/main/java/com/example/entity/RestBean.java b/study-project-backend/src/main/java/com/example/entity/RestBean.java new file mode 100644 index 0000000..a2a52e0 --- /dev/null +++ b/study-project-backend/src/main/java/com/example/entity/RestBean.java @@ -0,0 +1,44 @@ +package com.example.entity; + +import lombok.Data; + +@Data +public class RestBean { + private int status; + private boolean success; + private T message; + + public int getStatus() { + return status; + } + + public boolean isSuccess() { + return success; + } + + public T getMessage() { + return message; + } + + public RestBean(int status, boolean success, T message) { + this.status = status; + this.success = success; + this.message = message; + } + + public static RestBean success() { + return new RestBean<>(200,true,null); + } + + public static RestBean success(T data) { + return new RestBean<>(200,true,data); + } + + public static RestBean failure(int status) { + return new RestBean<>(status,false,null); + } + + public static RestBean failure(int status,T data) { + return new RestBean<>(status,false,data); + } +} diff --git a/study-project-backend/src/main/resources/application.yml b/study-project-backend/src/main/resources/application.yml index 2fdeda5..5dd7080 100644 --- a/study-project-backend/src/main/resources/application.yml +++ b/study-project-backend/src/main/resources/application.yml @@ -1 +1,6 @@ -spring.application.name=study-project-backend +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/study + username: root + password: root \ No newline at end of file