spring拦截器覆盖_SpringBoot自定义配置以及拦截器配置

本文介绍了如何在SpringBoot项目中自定义配置,包括通过@EnableWebMvc完全控制MVC配置,以及通过继承WebMvcConfigurerAdapter进行部分配置。详细讲解了自定义资源映射、页面跳转和拦截器的配置方法,提供了具体的代码示例,帮助读者理解如何实现SpringBoot应用中的个性化设置。
摘要由CSDN通过智能技术生成

在进行 SpringBoot 项目开发的过程中,对于 SpringBoot 自带的默认配置我们难免有自己不喜欢的地方。或者你认为更合理更想要的处理方式,这种时候你就可以选择配置自己的处理逻辑。

如果Spring Boot提供的Sping MVC不符合要求,则可以通过一个配置类(注解有@Configuration的类)加上@EnableWebMvc注解来实现完全自己控制的MVC配置。

@EnableWebMvc

@Configuration

public class TestMvc {

···

}

通常情况下,Spring Boot的自动配置是符合我们大多数需求的。在你既需要保留Spring Boot提供的便利,有需要增加自己的额外的配置的时候,可以定义一个配置类并继承WebMvcConfigurerAdapter,无需使用@EnableWebMvc注解。

@Configuration

public class TestMvc extends WebMvcConfigurerAdapter{

···

}

这里我们提到这个WebMvcConfigurerAdapter这个类,重写这个类中的方法可以让我们增加额外的配置,常用的有如下几个。

自定义资源映射 addResourceHandlers

如果想自定义静态资源映射目录的话,只需重写WebMvcConfigurerAdapter类的addResourceHandlers方法即可。

/**

* {@inheritDoc}

*

This implementation is empty.

*

* @param registry

*/

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/illuos1ion/**").addResourceLocations("classpath:/illuos1ion/");

super.addResourceHandlers(registry);

}

/ ========================================================================================= /

// WebMvcAutoConfiguration

public void addResourceHandlers(ResourceHandlerRegistry registry) {

if(!this.resourceProperties.isAddMappings()) {

logger.debug("Default resource handling disabled");

} else {

Integer cachePeriod = this.resourceProperties.getCachePeriod();

if(!registry.hasMappingForPattern("/webjars/**")) {

this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(cachePeriod));

}

String staticPathPattern = this.mvcProperties.getStaticPathPattern();

if(!registry.hasMappingForPattern(staticPathPattern)) {

this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));

}

}

}

通过addResourceHandler添加映射路径,然后通过addResourceLocations来指定资源文件路径。

访问自定义illuos1ion文件夹中的 item-video.png 图片的地址为 http://localhost:8080/illuos1ion/item-video.png

如果想指定外部的文件目录也很简单,直接通过addResourceLocations方法指定即可:

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/my/**").addResourceLocations("file:E:/illuos1ion/");

super.addResourceHandlers(registry);

}

addResourceLocations 指的是文件放置的目录,addResoureHandler 指的是对外暴露的访问路径

页面跳转 addViewControllers

重写 WebMvcConfigurerAdapter 中的 addViewControllers 方法即可达到你想要的处理效果:

/**

* 过去要访问一个页面需要先创建个Controller控制类,再写方法跳转到页面

* 在这里配置后就不需要那么麻烦了,直接访问http://localhost:8080/toLogin就跳转到login.jsp页面了

* @param registry

*/

@Override

public void addViewControllers(ViewControllerRegistry registry) {

registry.addViewController("/toLogin").setViewName("login");

super.addViewControllers(registry);

}

需要注意的是,在这里重写addViewControllers方法,并不会覆盖WebMvcAutoConfiguration中的addViewControllers(在此方法中,Spring Boot将“/”映射至index.html),这也就意味着我们自己的配置和Spring Boot的自动配置同时有效。

拦截器addInterceptors

拦截器在项目中经常使用的,这里介绍下最简单的判断是否登录的使用。

要实现拦截器功能需要完成2个步骤:

创建自己的拦截器类并实现 HandlerInterceptor 接口

重写WebMvcConfigurerAdapter类中的addInterceptors方法把自定义的拦截器类添加进来即可

拦截器代码:

@Component

public class LoginInterceptor implements HandlerInterceptor {

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

boolean flag;

User user = (User) request.getSession().getAttribute("user");

if (user == null) {

response.sendRedirect("/login");

flag = false;

} else {

flag = true;

}

return flag;

}

@Override

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

}

@Override

public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

}

}

这里简单实现了根据session中是否有User对象来判断是否登录,为空就跳转到登录页,不为空就通过。

接着,重写WebMvcConfigurerAdapter类中的addInterceptors方法:

Web配置代码:

@Configuration

public class WebMvcConfiguration extends WebMvcConfigurerAdapter{

@Autowired

LoginInterceptor loginInterceptor;

@Override

public void addInterceptors(InterceptorRegistry registry) {

// addPathPatterns 用于添加拦截规则

// excludePathPatterns 用户排除拦截

// 映射为 user 的控制器下的所有映射

registry.addInterceptor(loginInterceptor).addPathPatterns("/login/home").excludePathPatterns("/index", "/");

super.addInterceptors(registry);

}

}

addPathPatterns("/login/home")对/login/home请求拦截,但是排除了/index和/请求的拦截。

Html登录代码:

Login

用户名:

密 码:

登 录

注 册

控制器代码:

@Controller

@RequestMapping(path = "/login")

public class LoginController {

@RequestMapping(path = "/process", method = RequestMethod.POST)

public String login(@RequestParam String username,

@RequestParam String password,

HttpServletRequest request) {

if (username.equals("admin") && password.equals("admin")) {

User user = new User(username, password, "http://www.jiyongguang.xin");

request.getSession().setAttribute("user", user);

return "redirect:/login/home";

}

return "redirect:/";

}

@RequestMapping(path = "/home", method = RequestMethod.GET)

public String home(Model model) {

List userList = new ArrayList();

User user = new User("jyg", "jyg", "http://www.jiyongguang.xin");

userList.add(user);

user = new User("lyn", "lyn", "http://www.jiyongguang.xin");

userList.add(user);

model.addAttribute("userList", userList);

return "home";

}

}

或者可以通过Jquery封装好的AJAX方法来执行一套请求。效果是一样的

$(document).ready(function () {

$("#login").click(function () {

$.ajax({

type: "POST",

url: "/login/process",

data: {

username: $("#username").val(),

password: $("#password").val()

},

dataType: "json",

success: function (data) {

if (data.code == 1)

window.location.href = "/login/home";

else

alert("账号密码不能为空!");

}

});

});

});

控制器代码:

@RequestMapping(value = "/process", method = RequestMethod.POST)

@ResponseBody

public String index(@RequestParam String username,

@RequestParam String password,

HttpServletRequest request) {

Map map = new HashMap();

if (username.equals("admin") && password.equals("admin")) {

User user = new User(username, password, "http://www.jiyongguang.xin");

request.getSession().setAttribute("user", user);

return JsonUtil.getJsonString(JsonUtil.REQUEST_SUCCESS);

} else {

return JsonUtil.getJsonString(JsonUtil.REQUEST_Fail, "用户名或密码错误");

}

}

这样访问/login/home的时候,如果未登录就会跳转到login.html页面(注意缓存),而访问http://localhost:8080/index和http://localhost:8080不会被拦截。

更多配置可以查看WebMvcConfigurerAdapter的类的API。因其是WebMvcConfigurer接口的实现,所以WebMvcConfigurer的API方法也可以用来配置MVC。只是实现这个接口的话,要实现所有的方法,这个就比较麻烦了。所以还是推荐使用继承WebMvcConfigurerAdapter类来处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot允许我们自定义拦截器来拦截请求并进行一些逻辑处理。下面是SpringBoot自定义拦截器的步骤: 1.创建一个拦截器类并实现HandlerInterceptor接口,该接口有三个方法:preHandle、postHandle和afterCompletion。其中preHandle方法在请求处理之前被调用,postHandle方法在请求处理之后被调用,afterCompletion方法在视图渲染之后被调用。 2.在拦截器类上使用@Component注解将其注入到Spring容器中。 3.创建一个配置类并实现WebMvcConfigurer接口,该接口有一个addInterceptors方法,可以用来添加自定义拦截器。 4.在addInterceptors方法中添加自定义拦截器,并指定拦截的路径。 下面是一个简单的示例: 1.创建一个拦截器类并实现HandlerInterceptor接口: ```java @Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 在请求处理之前进行逻辑处理 // 如果返回false,则请求不会被处理 return true; } } ``` 2.创建一个配置类并实现WebMvcConfigurer接口: ```java @Configuration public class MyWebMvcConfigurer implements WebMvcConfigurer { @Autowired private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { // 添加自定义拦截器,并指定拦截路径 registry.addInterceptor(myInterceptor).addPathPatterns("/**"); } } ``` 在上面的示例中,我们创建了一个名为MyInterceptor的拦截器类,并将其注入到Spring容器中。然后,我们创建了一个名为MyWebMvcConfigurer的配置类,并在其中添加了我们的自定义拦截器,并指定了拦截的路径为“/**”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值