新增锁屏功能

This commit is contained in:
郑杰
2018-10-14 11:51:43 +08:00
parent 1f96fb2a43
commit 65b06947ce
23 changed files with 5361 additions and 7 deletions
+1
View File
@@ -3,6 +3,7 @@ package me.aurora;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -10,13 +10,16 @@ import me.aurora.repository.spec.UserSpec;
import me.aurora.service.RoleService;
import me.aurora.service.UserService;
import me.aurora.service.mapper.UserMapper;
import me.aurora.util.EncryptHelper;
import me.aurora.util.HttpContextUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
@@ -150,6 +153,47 @@ public class UserController {
return ResponseEntity.ok();
}
/**
* 锁屏页面
* @return
*/
@GetMapping(value = "/lock.html")
public ModelAndView lockPage(HttpServletRequest request){
User user = (User) SecurityUtils.getSubject().getPrincipal();
request.setAttribute("user",user);
return new ModelAndView("/user/lock");
}
/**
* 锁屏
* @return
*/
@GetMapping(value = "/lock")
public ResponseEntity lock(HttpServletRequest request,@RequestParam String url){
request.getSession(true).setAttribute("lock",url);
return ResponseEntity.ok();
}
/**
* 解锁
* @return
*/
@PostMapping(value = "/unlock")
public ResponseEntity unlock(@RequestParam String password){
User user = (User) SecurityUtils.getSubject().getPrincipal();
password = EncryptHelper.encrypt(password);
if(password.equals(user.getPassword())){
/**
* 将锁屏清掉
*/
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
String url = request.getSession(true).getAttribute("lock").toString();
request.getSession(true).setAttribute("lock",null);
return ResponseEntity.ok(url);
}
return ResponseEntity.error("密码错误");
}
/**
* 删除User
* @param id
@@ -40,6 +40,11 @@ public class UserSecurityController {
Subject subject = SecurityUtils.getSubject();
try {
subject.login(token);
/**
* 登录成功后将锁屏清掉
*/
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
request.getSession(true).setAttribute("lock",null);
return ResponseEntity.ok();
} catch (UnknownAccountException e) {
return ResponseEntity.error(e.getMessage());
@@ -1,6 +1,8 @@
package me.aurora.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@@ -11,6 +13,11 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class ConfigurerAdapter implements WebMvcConfigurer {
@Bean
LockInterceptor lockInterceptor(){
return new LockInterceptor();
}
/**
* 以前要访问一个页面需要先创建个Controller控制类,再写方法跳转到页面
* 在这里配置后就不需要那么麻烦了,直接访问http://localhost/login.html就跳转到login页面了
@@ -32,4 +39,12 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
registry.addViewController("/login.html").setViewName("/user/login");
registry.addViewController("/signUp.html").setViewName("/register");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
/**
* 添加锁屏的拦截器,并添加过滤规则
*/
registry.addInterceptor(new LockInterceptor()).addPathPatterns("/**").excludePathPatterns("/login.html","/user/lock.html","/logout","/login","/user/unlock","/css/**","/js/**","/plugins/**","/swagger/**");
}
}
@@ -0,0 +1,31 @@
package me.aurora.config;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LockInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession(true);
if(session.getAttribute("lock") != null){
response.sendRedirect(request.getContextPath()+"/user/lock.html");
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}