SpringBoot过滤器过滤未登录操作的页面

public class UserTokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userToken= request.getHeader("userToken");
        boolean ans=JWTUtil.verify(userToken);
        if(!ans){
            PrintWriter printWriter=response.getWriter();
            CommonData commonData=new CommonData(403,"Token验证失效,请重新登录!");
            String json=new ObjectMapper().writeValueAsString(commonData);
            printWriter.write(json);
            printWriter.close();
        }
        return ans;
    }
}

import com.ffyc.news.util.AdminTokenInterceptor;
import com.ffyc.news.util.UserTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration=registry.addInterceptor(new AdminTokenInterceptor());
        interceptorRegistration.addPathPatterns("/admin/**");
        interceptorRegistration.excludePathPatterns("/admin/login/login");
        InterceptorRegistration UserinterceptorRegistration=registry.addInterceptor(new UserTokenInterceptor());
        UserinterceptorRegistration.addPathPatterns("/user/**");
        UserinterceptorRegistration.excludePathPatterns("/user/user/userRegist/");
        UserinterceptorRegistration.excludePathPatterns("/user/user/userInfo/");
        UserinterceptorRegistration.excludePathPatterns("/user/user/findInfo/");
        UserinterceptorRegistration.excludePathPatterns("/user/user/changeState/");
        UserinterceptorRegistration.excludePathPatterns("/user/user/userLogin/");
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//
//    }
}

你可能感兴趣的:(后端,JAVA,Spring,spring,boot,java,spring)