springboot中配置“登陆拦截器”

在登陆处理中往往需要配置拦截器,用户只能从登陆页面中访问后台数据,要想实现这个功能只能通过拦截器来实现,
首先先创建一个拦截类,这个拦截类实现HandlerInterceptor接口,


public class MyInterceptor implements HandlerInterceptor {

    //springboot拦截器
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (request.getSession().getAttribute("userName") != null){
            return true;
        }else {
            request.getRequestDispatcher("/sign.html").forward(request,response);
            return false;
        }


    }

如果session没有数据的话,是转发到sign.html中的,但是这里没有自定义视图跳转是跳转不到templates的.html页面的,所以这里还需要自定义一个视图跳转。

//自定义视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/sign.html").setViewName("sign");
    }

然后在创建一个配置类,把需要放行的页面进行设置


@Configuration
public class MyConfig implements WebMvcConfigurer {
//自定义视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/sign.html").setViewName("sign");
    }

    @Bean
    public HandlerInterceptor handlerInterceptor(){
        return new loginInterceptor();
    }

    //配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(handlerInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/User/TO","/User/index","/sign.html","/User/Login");
    }
}

>  .excludePathPatterns("/index.html","/sign.html","/User/Login");
>  这些是需要放行的页面,拦截器是固定死的,只需要把放行路径改动即可。

只需要两步,springboot的拦截器就已经配置完成了。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot使用Mybatis拦截器,需要进行以下步骤: 1.定义拦截器类,实现Mybatis的Interceptor接口。 ```java @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}) }) public class MyInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { //拦截逻辑 return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { //设置拦截器属性 } } ``` 2.在Spring Boot配置文件配置拦截器。 ```yaml mybatis: configuration: #配置拦截器 #注意:mybatis下的configuration属性是Mybatis的Configuration对象,不是Spring Boot配置文件 #使用Mybatis的配置文件时需要使用mybatis.config-location属性 #使用Spring Boot配置文件时需要使用mybatis.configuration属性 #两者不能同时使用 plugins: - com.example.MyInterceptor ``` 3.在Mybatis的Mapper接口使用@Intercepts注解指定拦截器。 ```java @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}) }) public interface UserMapper { List<User> selectAll(); } ``` 其,@Intercepts注解用于指定拦截器,@Signature注解用于指定拦截的方法。 总的来说,在Spring Boot使用Mybatis拦截器的过程和在其他环境下使用Mybatis拦截器的过程类似,只需要在Spring Boot配置文件配置拦截器即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值