SpringBoot拦截器可以做什么可以对URL路径进行拦截,可以用于权限验证、解决乱码、操作日志记录、性能监控、异常处理等。SpringBoot拦截器实现登录拦截pom.xml: 4.0.0 org.s
可以对URL路径进行拦截,可以用于权限验证、解决乱码、操作日志记录、性能监控、异常处理等。
SpringBoot拦截器实现登录拦截
pom.xml:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
com.wyj
SpringBoot-interceptor01
0.0.1-SNAPSHOT
springboot-interceptor01
springboot拦截器
1.8
org.springframework.boot
spring-boot-starter-WEB
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-thymeleaf
springboot-interceptor01
org.springframework.boot
spring-boot-Maven-plugin
WebMvcConfigurer:继承WebmvcConfigurationSupport类,重写addInterceptors方法
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
// @Override
// public void addInterceptors(InterceptorReGIStry registry) {
//
// registry.addInterceptor(new AuthorityInterceptor())
// .addPathPatterns("/user
// @Override
// public void addInterceptors(InterceptorRegistry registry) {
//
// registry.addInterceptor(new AuthorityInterceptor())
// .addPathPatterns("/user
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthorityInterceptor())
.addPathPatterns("/user
@Override
public boolean preHandle(httpservletRequest request, HttpServletResponse response,
Object object) throws Exception {
String uri = request.getRequestURI();
if (NOT_INTERCEPT_URI.contains(uri)) {
log.info("不拦截" + uri);
return true;
}
log.info("拦截" + uri);
HttpSession session = request.getSession();
UserInfo userInfo = (UserInfo) session.getAttribute("user_info_in_the_session");
if (userInfo == null) {
throw new RuntimeException("用户未登陆");
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView mv) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception ex) throws Exception {
}
}
UserController:
@Controller
@RequestMapping(value = "/user")
public class UserController {
@RequestMapping(value = "/login.html")
public String index() {
return "login";
}
@RequestMapping(value = "/login")
public String login(User user) {
//查询数据库,我这里直接写死
User dbUser = new User(1, "zhangsan", "123456", "admin");
if (dbUser.getPassword().equals(user.getPassword())) {
UserInfo userInfo = new UserInfo(dbUser.getId(), dbUser.getUsername(), dbUser.getRole());
HttpSession session = getRequest().getSession();
session.setAttribute("user_info_in_the_session", userInfo);
return "admin";
}
return "login";
}
@RequestMapping(value = "/userInfo")
@ResponseBody
public String userInfo() {
HttpSession session = getRequest().getSession();
UserInfo userInfo = (UserInfo) session.getAttribute("user_info_in_the_session");
return userInfo.toString();
}
private HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
}
}
User:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private int id;
private String username;
private String passWord;
private String role;
}
UserInfo: 用于存在用户信息储存在session中
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserInfo implements Serializable {
private int id;
private String username;
private String role;
}
login.html:只是一个很简单的登录表单
登陆页面
admin.html:
首页
--结束END--
本文标题: SpringBoot拦截器实现登录拦截
本文链接: https://www.lsjlt.com/news/2454.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0