实现登录拦截器
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
package com.fjy.spring.constant;
|
||||||
|
|
||||||
|
public class GlobalConstant {
|
||||||
|
public static final String USER_SESSION_KEY = "USER_SESSION";
|
||||||
|
}
|
||||||
@@ -1,22 +1,36 @@
|
|||||||
package com.fjy.spring.controller;
|
package com.fjy.spring.controller;
|
||||||
|
|
||||||
import com.fjy.spring.domain.TbUser;
|
import com.fjy.spring.domain.TbUser;
|
||||||
|
import com.fjy.spring.properties.ServerProperties;
|
||||||
import com.fjy.spring.service.UserService;
|
import com.fjy.spring.service.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class LoginController {
|
public class LoginController {
|
||||||
|
@Autowired
|
||||||
|
private ServerProperties serverProperties;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
HttpServletRequest request;
|
||||||
|
|
||||||
@PostMapping("/login/dologin")
|
@PostMapping("/login/dologin")
|
||||||
public String doLogin(TbUser tbUser)throws Exception{
|
public String doLogin(TbUser tbUser)throws Exception{
|
||||||
if (userService.doLoginService(tbUser.getColname(),tbUser.getColpassword())){
|
TbUser user = userService.doLoginService(tbUser.getColname(),tbUser.getColpassword());
|
||||||
return "/home/home";
|
if (user!=null){
|
||||||
|
request.getSession().setAttribute(USER_SESSION_KEY,user);
|
||||||
|
return "redirect:" + request.getScheme() + "://" + request.getServerName() + ":"
|
||||||
|
+ serverProperties.getPortNum() + request.getContextPath() + "/home";
|
||||||
}
|
}
|
||||||
return "login";
|
return "login";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,4 +19,9 @@ public class NavController {
|
|||||||
public String toTestAxiosPage(){
|
public String toTestAxiosPage(){
|
||||||
return "/dist/axiosTest";
|
return "/dist/axiosTest";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping(value = {"/home"})
|
||||||
|
public String toHomePage(){
|
||||||
|
return "/home/home";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.fjy.spring.interceptor;
|
||||||
|
|
||||||
|
import com.fjy.spring.constant.GlobalConstant;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
public class LoginInterceptor implements HandlerInterceptor {
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
System.out.println("preHandle");
|
||||||
|
Object user = request.getSession().getAttribute(GlobalConstant.USER_SESSION_KEY);
|
||||||
|
if (user==null){
|
||||||
|
response.sendRedirect("/cms/index");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
||||||
|
System.out.println("postHandle");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
||||||
|
System.out.println("afterCompletion");
|
||||||
|
}
|
||||||
|
}
|
||||||
33
src/main/java/com/fjy/spring/interceptor/WebAppConfig.java
Normal file
33
src/main/java/com/fjy/spring/interceptor/WebAppConfig.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package com.fjy.spring.interceptor;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebAppConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册拦截器
|
||||||
|
* @param registry
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/home/**");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 排除静态资源
|
||||||
|
* @param registry
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
|
registry.addResourceHandler("/cms/js/**").addResourceLocations("classpath:/js/");
|
||||||
|
registry.addResourceHandler("/cms/css/**").addResourceLocations("classpath:/css/");
|
||||||
|
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
|
||||||
|
registry.addResourceHandler("/cms/fonts/**").addResourceLocations("classpath:/fonts/");
|
||||||
|
registry.addResourceHandler("/cms/images/**").addResourceLocations("classpath:/images/");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,11 +15,11 @@ public class UserService {
|
|||||||
private TbUserRepository tbUserRepository;
|
private TbUserRepository tbUserRepository;
|
||||||
|
|
||||||
|
|
||||||
public boolean doLoginService(String name,String password){
|
public TbUser doLoginService(String name,String password){
|
||||||
TbUser user = (TbUser)tbUserRepository.findByColname(name).get();
|
TbUser user = (TbUser)tbUserRepository.findByColname(name).get();
|
||||||
if (user!=null){
|
if (user!=null){
|
||||||
if (password.equals(user.getColpassword())){
|
if (password.equals(user.getColpassword())){
|
||||||
return true;
|
return user;
|
||||||
}else {
|
}else {
|
||||||
throw new UserException(ResultEnum.WRONGPASS);
|
throw new UserException(ResultEnum.WRONGPASS);
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,8 @@ public class UserService {
|
|||||||
|
|
||||||
TbUser user = tbUserRepository.save(tbUser);
|
TbUser user = tbUserRepository.save(tbUser);
|
||||||
if (user!=null){
|
if (user!=null){
|
||||||
throw new UserException(ResultEnum.SUCCESS);
|
//throw new UserException(ResultEnum.SUCCESS);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,7 @@
|
|||||||
</el-container>
|
</el-container>
|
||||||
</div>
|
</div>
|
||||||
<!--<div th:include="/dist/thymeleaf/footer :: copyright"></div>-->
|
<!--<div th:include="/dist/thymeleaf/footer :: copyright"></div>-->
|
||||||
<script th:src="@{../js/homePage.js}"></script>
|
<script th:src="@{js/homePage.js}"></script>
|
||||||
<script th:src="@{../js/msg.js}"></script>
|
<script th:src="@{js/msg.js}"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user