当前位置:实例文章 » JAVA Web实例» [文章]SpringBoot 统一功能处理:用户登录权限校验-拦截器、异常处理、数据格式返回

SpringBoot 统一功能处理:用户登录权限校验-拦截器、异常处理、数据格式返回

发布人:shili8 发布时间:2025-02-26 10:43 阅读次数:0

**SpringBoot 统一功能处理**

在 SpringBoot 应用中,为了提高开发效率和代码重复利用性,我们可以使用拦截器(Interceptor)、异常处理(Exception Handling)以及数据格式返回(Response Format)来统一处理一些常见的功能,如用户登录权限校验、异常捕捉等。

###1. 拦截器(Interceptor)

拦截器是一种用于过滤请求和响应的组件,可以在 SpringBoot 应用中使用来实现一些通用的功能,如用户登录权限校验、日志记录等。

#### **创建拦截器类**

java// UserLoginInterceptor.javaimport org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet. />import javax.servlet. />import java.util.Enumeration;

public class UserLoginInterceptor implements HandlerInterceptor {

 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 // 用户登录权限校验逻辑 String username = request.getParameter("username");
 String password = request.getParameter("password");

 if (username == null || password == null) {
 return false; // 未登录,返回false }

 // todo: 实现用户登录的业务逻辑 return true; // 登录成功,继续执行下一步 }

 @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
 // 响应后处理逻辑 }
}


#### **配置拦截器**

java// WebConfig.javaimport org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configurationpublic class WebConfig implements WebMvcConfigurer {

 @Override public void addInterceptors(InterceptorRegistry registry) {
 // 注册拦截器 registry.addInterceptor(new UserLoginInterceptor())
 .addPathPatterns("/**"); // 拦截所有请求 }
}


###2. 异常处理(Exception Handling)

异常处理是指在 SpringBoot 应用中捕捉和处理异常的逻辑。

#### **创建异常类**

java// CustomException.javapublic class CustomException extends RuntimeException {

 public CustomException(String message) {
 super(message);
 }
}


#### **配置异常处理器**

java// ExceptionConfig.javaimport org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Configurationpublic class ExceptionConfig {

 @Bean public CustomExceptionHandler customExceptionHandler() {
 return new CustomExceptionHandler();
 }
}


#### **异常处理器类**

java// CustomExceptionHandler.javaimport org.springframework. />import org.springframework. />import org.springframework.web.bind.annotation.ExceptionHandler;

public class CustomExceptionHandler {

 @ExceptionHandler(CustomException.class)
 public ResponseEntity handleCustomException(CustomException e) {
 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
 }
}


###3. 数据格式返回(Response Format)

数据格式返回是指在 SpringBoot 应用中统一处理响应的数据格式。

#### **创建响应类**

java// Response.javapublic class Response {

 private int code;
 private String message;
 private T data;

 public Response(int code, String message, T data) {
 this.code = code;
 this.message = message;
 this.data = data;
 }

 // getter和setter方法}


#### **配置响应处理器**

java// ResponseConfig.javaimport org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configurationpublic class ResponseConfig {

 @Bean public ResponseHandler responseHandler() {
 return new ResponseHandler();
 }
}


#### **响应处理器类**

java// ResponseHandler.javaimport org.springframework. />import org.springframework. />import org.springframework.web.bind.annotation.ResponseStatus;

public class ResponseHandler {

 @ResponseStatus(HttpStatus.OK)
 public ResponseEntity> handleResponse(Response response) {
 return ResponseEntity.ok(response);
 }
}


通过以上配置,SpringBoot 应用中可以统一处理用户登录权限校验、异常捕捉以及数据格式返回等功能。

其他信息

其他资源

Top