Spring mvc
使用拦截器控制登录
public class AuditingInterceptor extends HandlerInterceptorAdapter {
Logger logger = Logger.getLogger(AuditingInterceptor.class);
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String currentPath = request.getRequestURI();
String queryString = request.getQueryString();
queryString = queryString == null ? "" : "?" + queryString;
logger.info(currentPath + queryString);
Stu isLogin=(Stu) request.getSession().getAttribute("islogin");
if (isLogin == null) {
currentPath = request.getServletContext().getContextPath();
response.sendRedirect(currentPath + "/stu/login.action");
return false;
}
return true;
}
}
spring mvc配置拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/stu/login.action" />
<mvc:exclude-mapping path="/func1/showFunc1.action" />
<mvc:exclude-mapping path="/func1/ajaxsel.action" />
<bean class="com.springmy.interceptor.AuditingInterceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors>
spring boot
未配置
拦截器
package com.boothtml.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class AuditingInterceptor extends HandlerInterceptorAdapter {
Logger logger = Logger.getLogger(AuditingInterceptor.class);
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String currentPath = request.getRequestURI();
String queryString = request.getQueryString();
queryString = queryString == null ? "" : "?" + queryString;
logger.info(currentPath + queryString);
String isLogin=(String) request.getSession().getAttribute("islogin");
if (isLogin == null) {
currentPath = request.getServletContext().getContextPath();
response.sendRedirect(currentPath + "/testa");
return false;
}
return true;
}
}
配置拦截器
package com.boothtml;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.boothtml.interceptor.AuditingInterceptor;
@Controller
@SpringBootApplication
@MapperScan(basePackages = "com.boothtml.mapper")
@EnableCaching
public class BootHtml implements WebMvcConfigurer {
Logger log = Logger.getLogger(BootHtml.class);
@RequestMapping("/testa")
@ResponseBody
String test() {
log.info("======================");
return "无权访问";
}
@RequestMapping("/")
String home(Model model, HttpServletRequest request) {
log.info("log4j properties home() to index...............");
model.addAttribute("req", "bootreqattr");
request.getSession().setAttribute("session", "session attr");
return "index";
}
public static void main(String[] args) {
SpringApplication.run(BootHtml.class, args);
}
@Value("${spring.servlet.multipart.location}")
String upload;
public void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("+++++++++ static resource config........................");
registry.addResourceHandler("/upload/**").addResourceLocations("file:///" + upload);
}
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuditingInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/testa")
.excludePathPatterns("/func1")
.excludePathPatterns(new String[] {"/*.html","/*.js","/jss/**"});
}
}
方法二
application.yml
入口
日志