Spring Bootユーザー権限ブロック登録
5378 ワード
1.カスタムスクリーンクラスを作成し、Handler Interceptorインターフェースを実現する
package com.xgf.online_mall.interceptor;
import com.xgf.online_mall.system.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
@Slf4j
@Component
public class UserLoginAuthInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info(" ======== UserAuthInterceptor preHandle ");
User user = (User) request.getSession().getAttribute("loginUser");
// ,
if(user == null){
//
String address = request.getRequestURI();
log.info("======== , address : {}", address);
response.sendRedirect(request.getContextPath() + "/login.html");
return false;
/*String address = request.getRequestURI();
log.info("======== , address : {}", address);
// ,
if(!address.contains("login") && !address.contains("register")){
//
response.sendRedirect(request.getContextPath() + "/login.html");
// false, controller
return false;
}*/
}
// ,
// log.info("======== user = {}", user);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info(" ===== UserAuthInterceptor postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("==== UserAuthInterceptor afterCompletion");
//
// log -- tomcat +log
String logdir = request.getServletContext().getRealPath("log");
//
Path logdirPath = Paths.get(logdir);
if(Files.notExists(logdirPath)){
Files.createDirectories(logdirPath);
}
// [ ] // +
Path logfile = Paths.get(logdir,"userlog.log");
//logfile.toFile() paths File true
BufferedWriter writer = new BufferedWriter(new FileWriter(logfile.toFile(),true));
//
User user = (User)request.getSession().getAttribute("loginUser");
// user ,
writer.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + " >> " + user +"\r
");
writer.flush();
writer.close();
}
}
2.WebMvcConfigrerインターフェースの実現類を作成し、ユーザー定義のスクリーンセーバーを登録して有効にする。
import com.xgf.online_mall.constant.PathConstantParam;
import com.xgf.online_mall.interceptor.UserLoginAuthInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
@Configuration
@Slf4j
public class LoginConfig implements WebMvcConfigurer {
@Autowired
private UserLoginAuthInterceptor userLoginAuthInterceptor;
/**
* addInterceptors
* addPathPatterns:
* excludePathPatterns: ,
* String ","
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry){
log.info(" ======== LoginConfig.addInterceptors");
// ,
//error ,excludePathPatterns ,
//springboot /error, 。
registry.addInterceptor(userLoginAuthInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/js/**", "/css/**", "/img/**", "/plugins/**")
.excludePathPatterns("/login.html", "/register.html", "/system/user/login", "/system/user/login", "/index")
.excludePathPatterns("/error");
}
}
ここで、SpringBoot登録ユーザー権限ブロックに関する記事を紹介します。Spring Bootに関するユーザー権限ブロックの内容は以前の記事を検索したり、下記の関連記事を見たりしてください。これからもよろしくお願いします。