RBAC设计完成、拦截器、AOP、视图等测试通过
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
package top.fjy8018.fileupload.aspect;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Before;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
import top.fjy8018.fileupload.constant.GlobalConstant;
|
||||||
|
import top.fjy8018.fileupload.dataobject.User;
|
||||||
|
import top.fjy8018.fileupload.dataobject.view.UserPermission;
|
||||||
|
import top.fjy8018.fileupload.enums.PercodeEnum;
|
||||||
|
import top.fjy8018.fileupload.enums.ResultVOEnum;
|
||||||
|
import top.fjy8018.fileupload.exception.FileUploadException;
|
||||||
|
import top.fjy8018.fileupload.service.UserService;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传权限校验
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-06-23 16:13
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public class UploadAuthorizeAspect {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 过滤所有上传操作
|
||||||
|
*/
|
||||||
|
@Pointcut("execution(public * top.fjy8018.fileupload.controller.UploadController.moreUpload(..))")
|
||||||
|
public void verify(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before("verify()")
|
||||||
|
public void doVerify(){
|
||||||
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
|
HttpServletRequest request = attributes.getRequest();
|
||||||
|
|
||||||
|
User user = (User)request.getSession().getAttribute(GlobalConstant.USER_SESSION_KEY);
|
||||||
|
if (user==null){
|
||||||
|
throw new FileUploadException(ResultVOEnum.LOG_OUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
UserPermission userPermission = userService.findPermissionByUserName(user.getUsername());
|
||||||
|
String[] percodes = userPermission.getPercode().split(":");
|
||||||
|
|
||||||
|
log.info("【上传AOP】percodes:{},userPermission:{}",percodes[1],userPermission);
|
||||||
|
|
||||||
|
if (!percodes[1].equals(PercodeEnum.FILE_UPLOAD.getMsg())){
|
||||||
|
throw new FileUploadException(ResultVOEnum.FORBIDDEN_ACCES);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package top.fjy8018.fileupload.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import top.fjy8018.fileupload.dataobject.User;
|
||||||
|
import top.fjy8018.fileupload.form.LoginForm;
|
||||||
|
import top.fjy8018.fileupload.service.UserService;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import static top.fjy8018.fileupload.constant.GlobalConstant.USER_SESSION_KEY;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-06-23 16:34
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Controller
|
||||||
|
public class LoginController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
HttpServletRequest request;
|
||||||
|
|
||||||
|
@GetMapping(value = {"/index","login"})
|
||||||
|
public String toLoginPage(){
|
||||||
|
return "index";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/dologin")
|
||||||
|
public String doLogin(LoginForm loginForm) {
|
||||||
|
|
||||||
|
log.info("【loginForm】{}",loginForm);
|
||||||
|
|
||||||
|
User user = userService.findOneByUserName(loginForm.getUserName());
|
||||||
|
if (user!=null){
|
||||||
|
request.getSession().setAttribute(USER_SESSION_KEY,user);
|
||||||
|
}
|
||||||
|
|
||||||
|
return "home/upload.html";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package top.fjy8018.fileupload.dataobject.view;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.hibernate.annotations.Immutable;
|
||||||
|
import org.hibernate.annotations.Subselect;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-06-23 18:25
|
||||||
|
*/
|
||||||
|
@Entity
|
||||||
|
@Immutable
|
||||||
|
@Subselect("SELECT * FROM v_sys_user_permission")
|
||||||
|
@Data
|
||||||
|
public class UserPermission {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
private String percode;
|
||||||
|
|
||||||
|
private Integer parentid;
|
||||||
|
}
|
||||||
18
src/main/java/top/fjy8018/fileupload/enums/PercodeEnum.java
Normal file
18
src/main/java/top/fjy8018/fileupload/enums/PercodeEnum.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package top.fjy8018.fileupload.enums;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum PercodeEnum implements CodeEnum{
|
||||||
|
FILE_UPLOAD(3,"upload"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private Integer code;
|
||||||
|
|
||||||
|
private String msg;
|
||||||
|
|
||||||
|
PercodeEnum(Integer code, String msg) {
|
||||||
|
this.code = code;
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,8 @@ package top.fjy8018.fileupload.enums;
|
|||||||
public enum ResultVOEnum implements CodeEnum {
|
public enum ResultVOEnum implements CodeEnum {
|
||||||
UPLOAD_SUCCESS(0,"上传成功"),
|
UPLOAD_SUCCESS(0,"上传成功"),
|
||||||
UPLOAD_FAIL(-1,"上传失败"),
|
UPLOAD_FAIL(-1,"上传失败"),
|
||||||
|
FORBIDDEN_ACCES(1,"没有访问权限"),
|
||||||
|
LOG_OUT(2,"未登录"),
|
||||||
;
|
;
|
||||||
|
|
||||||
private Integer code;
|
private Integer code;
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package top.fjy8018.fileupload.interceptor;
|
||||||
|
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
import top.fjy8018.fileupload.constant.GlobalConstant;
|
||||||
|
|
||||||
|
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 {
|
||||||
|
Object user = request.getSession().getAttribute(GlobalConstant.USER_SESSION_KEY);
|
||||||
|
if (user==null){
|
||||||
|
response.sendRedirect("/fileupload/index");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package top.fjy8018.fileupload.interceptor;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
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 WebAppConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册拦截器
|
||||||
|
* @param registry
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/home/**").addPathPatterns("/upload/**");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 排除静态资源
|
||||||
|
* @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/");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,4 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||||||
import top.fjy8018.fileupload.dataobject.User;
|
import top.fjy8018.fileupload.dataobject.User;
|
||||||
|
|
||||||
public interface UserRepository extends JpaRepository<User,String> {
|
public interface UserRepository extends JpaRepository<User,String> {
|
||||||
|
|
||||||
|
User findByUsername(String userName);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package top.fjy8018.fileupload.repository.view;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import top.fjy8018.fileupload.dataobject.view.UserPermission;
|
||||||
|
|
||||||
|
public interface UserPermissionRepository extends JpaRepository<UserPermission,String> {
|
||||||
|
|
||||||
|
UserPermission findByUsername(String userName);
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package top.fjy8018.fileupload.service;
|
||||||
|
|
||||||
|
import top.fjy8018.fileupload.dataobject.User;
|
||||||
|
import top.fjy8018.fileupload.dataobject.view.UserPermission;
|
||||||
|
|
||||||
|
public interface UserService {
|
||||||
|
|
||||||
|
User findOneByUserName(String userName);
|
||||||
|
|
||||||
|
UserPermission findPermissionByUserName(String userName);
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package top.fjy8018.fileupload.service.impl;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import top.fjy8018.fileupload.dataobject.User;
|
||||||
|
import top.fjy8018.fileupload.dataobject.view.UserPermission;
|
||||||
|
import top.fjy8018.fileupload.repository.UserRepository;
|
||||||
|
import top.fjy8018.fileupload.repository.view.UserPermissionRepository;
|
||||||
|
import top.fjy8018.fileupload.service.UserService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2018-06-23 16:37
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserPermissionRepository userPermissionRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserPermission findPermissionByUserName(String userName) {
|
||||||
|
return userPermissionRepository.findByUsername(userName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User findOneByUserName(String userName) {
|
||||||
|
return userRepository.findByUsername(userName);
|
||||||
|
}
|
||||||
|
}
|
||||||
47
src/main/resources/static/js/login.js
Normal file
47
src/main/resources/static/js/login.js
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
var Main = {
|
||||||
|
data() {
|
||||||
|
var checkUserName = (rule, value, callback) => {
|
||||||
|
if (!value) {
|
||||||
|
return callback(new Error('用户名不能为空'));
|
||||||
|
}
|
||||||
|
callback();
|
||||||
|
};
|
||||||
|
var validatePass = (rule, value, callback) => {
|
||||||
|
if (value === '') {
|
||||||
|
callback(new Error('请输入密码'));
|
||||||
|
}
|
||||||
|
callback();
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
ruleForm2: {
|
||||||
|
password: '',
|
||||||
|
userName: ''
|
||||||
|
},
|
||||||
|
rules2: {
|
||||||
|
password: [
|
||||||
|
{validator: validatePass, trigger: 'blur'}
|
||||||
|
],
|
||||||
|
userName: [
|
||||||
|
{validator: checkUserName, trigger: 'blur'}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
submitForm(formName) {
|
||||||
|
this.$refs[formName].validate((valid) => {
|
||||||
|
if (valid) {
|
||||||
|
document.getElementById('loginForm').submit();
|
||||||
|
} else {
|
||||||
|
console.log('error submit!!');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
resetForm(formName) {
|
||||||
|
this.$refs[formName].resetFields();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var Ctor = Vue.extend(Main)
|
||||||
|
new Ctor().$mount('#app')
|
||||||
26
src/main/resources/templates/index.html
Normal file
26
src/main/resources/templates/index.html
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head th:include="dist/thymeleaf/common_head :: header('登录')">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div th:insert="~{dist/thymeleaf/common_head :: #body_js}"></div>
|
||||||
|
<div id="app">
|
||||||
|
<h1>登录</h1>
|
||||||
|
<el-form :model="ruleForm2" status-icon :rules="rules2" id="loginForm" action="/fileupload/dologin"
|
||||||
|
method="POST" name="loginForm" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
|
||||||
|
<el-form-item label="用户名" prop="userName">
|
||||||
|
<el-input v-model.number="ruleForm2.userName" name="userName"></el-input>
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item label="密码" prop="password">
|
||||||
|
<el-input type="password" v-model="ruleForm2.password" name="password" auto-complete="off"></el-input>
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item>
|
||||||
|
<el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
|
||||||
|
<el-button @click="resetForm('ruleForm2')">重置</el-button>
|
||||||
|
</el-form-item>
|
||||||
|
</el-form>
|
||||||
|
</div>
|
||||||
|
<script th:src="@{/js/login.js}+'?v=0.1'"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package top.fjy8018.fileupload.repository;
|
package top.fjy8018.fileupload.repository;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -12,6 +13,7 @@ import javax.transaction.Transactional;
|
|||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class UserRepositoryTest extends EstestApplicationTests{
|
public class UserRepositoryTest extends EstestApplicationTests{
|
||||||
|
|
||||||
@@ -42,5 +44,12 @@ public class UserRepositoryTest extends EstestApplicationTests{
|
|||||||
Assert.assertNotNull(res);
|
Assert.assertNotNull(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findByUsername(){
|
||||||
|
User res = repository.findByUsername("admin");
|
||||||
|
log.info("【用户信息】{}",res.toString());
|
||||||
|
Assert.assertNotNull(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package top.fjy8018.fileupload.repository.view;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import top.fjy8018.fileupload.EstestApplicationTests;
|
||||||
|
import top.fjy8018.fileupload.dataobject.view.UserPermission;
|
||||||
|
import top.fjy8018.fileupload.repository.UserRepository;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class UserPermissionRepositoryTest extends EstestApplicationTests{
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserPermissionRepository repository;
|
||||||
|
|
||||||
|
private static final String USER_NAME="admin";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findByUsername(){
|
||||||
|
UserPermission userPermission = repository.findByUsername(USER_NAME);
|
||||||
|
log.info("【findByUsername】{}",userPermission);
|
||||||
|
|
||||||
|
Assert.assertNotNull(userPermission);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user