first update
This commit is contained in:
parent
571bfc27b3
commit
dc52d7701d
8
pom.xml
8
pom.xml
@ -32,10 +32,10 @@
|
||||
<mapstruct.version>1.6.2</mapstruct.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-starter-data-mongodb</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
|
@ -41,28 +41,28 @@ public class FirstTimeInitializer implements CommandLineRunner {
|
||||
|
||||
public void run(String... args) {
|
||||
if (authorityService.count() == 0L) {
|
||||
Authority userPerm = new Authority(null, "用户模块", "用户管理", "USER", true, null);
|
||||
Authority rolePerm = new Authority(null, "角色模块", "角色管理", "ROLE", true, null);
|
||||
Authority deptPerm = new Authority(null, "部门模块", "部门管理", "DEPT", true, null);
|
||||
Authority AuthorityPerm = new Authority(null, "权限模块", "权限管理", "Authority", true, null);
|
||||
Authority userPerm = new Authority(null, "用户模块", "用户管理", "SYSTEM:USER", true, null);
|
||||
Authority rolePerm = new Authority(null, "角色模块", "角色管理", "SYSTEM:ROLE", true, null);
|
||||
Authority deptPerm = new Authority(null, "部门模块", "部门管理", "SYSTEM:DEPT", true, null);
|
||||
Authority AuthorityPerm = new Authority(null, "权限模块", "权限管理", "SYSTEM:AUTHORITY", true, null);
|
||||
authorityService.saveBatch(List.of(userPerm, rolePerm, deptPerm, AuthorityPerm));
|
||||
List<Authority> authorities = new ArrayList<>();
|
||||
authorities.add(new Authority(userPerm.getId(), "用户管理", "用户列表", "USER_QUERY", true, null));
|
||||
authorities.add(new Authority(userPerm.getId(), "用户管理", "用户新增", "USER_CREATE",true, null));
|
||||
authorities.add(new Authority(userPerm.getId(), "用户管理", "用户修改", "USER_UPDATE",true, null));
|
||||
authorities.add(new Authority(userPerm.getId(), "用户管理", "用户删除", "USER_DELETE",true, null));
|
||||
authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色列表", "ROLE_QUERY", true, null));
|
||||
authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色新增", "ROLE_CREATE",true, null));
|
||||
authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色修改", "ROLE_UPDATE",true, null));
|
||||
authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色删除", "ROLE_DELETE",true, null));
|
||||
authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门列表", "DEPT_QUERY", true, null));
|
||||
authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门新增", "DEPT_CREATE",true, null));
|
||||
authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门修改", "DEPT_UPDATE",true, null));
|
||||
authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门删除", "DEPT_DELETE",true, null));
|
||||
authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限列表", "AUTHORITY_QUERY", true, null));
|
||||
authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限新增", "AUTHORITY_CREATE",true, null));
|
||||
authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限修改", "AUTHORITY_UPDATE",true, null));
|
||||
authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限删除", "AUTHORITY_DELETE",true, null));
|
||||
authorities.add(new Authority(userPerm.getId(), "用户管理", "用户列表", "SYSTEM:USER:QUERY", true, null));
|
||||
authorities.add(new Authority(userPerm.getId(), "用户管理", "用户新增", "SYSTEM:USER:CREATE",true, null));
|
||||
authorities.add(new Authority(userPerm.getId(), "用户管理", "用户修改", "SYSTEM:USER:UPDATE",true, null));
|
||||
authorities.add(new Authority(userPerm.getId(), "用户管理", "用户删除", "SYSTEM:USER:DELETE",true, null));
|
||||
authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色列表", "SYSTEM:ROLE:QUERY", true, null));
|
||||
authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色新增", "SYSTEM:ROLE:CREATE",true, null));
|
||||
authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色修改", "SYSTEM:ROLE:UPDATE",true, null));
|
||||
authorities.add(new Authority(rolePerm.getId(), "角色管理", "角色删除", "SYSTEM:ROLE:DELETE",true, null));
|
||||
authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门列表", "SYSTEM:DEPT:QUERY", true, null));
|
||||
authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门新增", "SYSTEM:DEPT:CREATE",true, null));
|
||||
authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门修改", "SYSTEM:DEPT:UPDATE",true, null));
|
||||
authorities.add(new Authority(deptPerm.getId(), "部门管理", "部门删除", "SYSTEM:DEPT:DELETE",true, null));
|
||||
authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限列表", "SYSTEM:AUTHORITY:QUERY", true, null));
|
||||
authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限新增", "SYSTEM:AUTHORITY:CREATE",true, null));
|
||||
authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限修改", "SYSTEM:AUTHORITY:UPDATE",true, null));
|
||||
authorities.add(new Authority(AuthorityPerm.getId(), "权限管理", "权限删除", "SYSTEM:AUTHORITY:DELETE",true, null));
|
||||
authorityService.saveBatch(authorities);
|
||||
}
|
||||
if (roleService.count() == 0L) {
|
||||
|
@ -1,13 +1,18 @@
|
||||
package com.zsc.edu.gateway.framework.security;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
|
||||
import org.springframework.security.web.csrf.CsrfTokenRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author harry_yao
|
||||
@ -16,12 +21,10 @@ import java.io.IOException;
|
||||
@Component
|
||||
public class CustomAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
||||
|
||||
// private final OnlineUserService onlineUserService;
|
||||
// private final UserService userService;
|
||||
// private final LoginLogService loginLogService;
|
||||
|
||||
@Override
|
||||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
|
||||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
|
||||
// response.sendRedirect("/api/rest/user/me");
|
||||
// request.getRequestDispatcher("/api/rest/user/me").forward(request, response);
|
||||
// Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||
// String sessionId = request.getRequestedSessionId();
|
||||
// String remoteAddr = request.getRemoteAddr();
|
||||
|
@ -14,10 +14,11 @@ import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
|
||||
|
||||
@Override
|
||||
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
|
||||
throws AuthenticationException {
|
||||
if (!request.getMethod().equals("POST")) {
|
||||
if (!request.getMethod().equals("POST")) {
|
||||
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
|
||||
}
|
||||
if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) {
|
||||
@ -30,7 +31,7 @@ public class JsonAuthenticationFilter extends UsernamePasswordAuthenticationFilt
|
||||
username = username.trim();
|
||||
password = (password != null) ? password : "";
|
||||
password = password.trim();
|
||||
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
|
||||
UsernamePasswordAuthenticationToken authRequest = UsernamePasswordAuthenticationToken.unauthenticated(username, password);
|
||||
// Allow subclasses to set the "details" property
|
||||
setDetails(request, authRequest);
|
||||
return this.getAuthenticationManager().authenticate(authRequest);
|
||||
|
@ -13,10 +13,10 @@ import org.springframework.security.web.session.HttpSessionEventPublisher;
|
||||
*/
|
||||
@Configuration
|
||||
public class SecurityBeanConfig {
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||
}
|
||||
// @Bean
|
||||
// public PasswordEncoder passwordEncoder() {
|
||||
// return PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||
// }
|
||||
|
||||
@Bean
|
||||
public SessionRegistry sessionRegistry() {
|
||||
|
@ -11,11 +11,16 @@ import org.springframework.security.authentication.dao.DaoAuthenticationProvider
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.core.session.SessionRegistry;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
|
||||
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
|
||||
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
|
||||
import org.springframework.security.web.csrf.CsrfTokenRepository;
|
||||
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@ -37,6 +42,11 @@ public class SpringSecurityConfig {
|
||||
@Resource
|
||||
private final DataSource dataSource;
|
||||
|
||||
@Bean
|
||||
public BCryptPasswordEncoder bCryptPasswordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
};
|
||||
|
||||
@Bean
|
||||
public PersistentTokenRepository persistentTokenRepository() {
|
||||
JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
|
||||
@ -48,7 +58,7 @@ public class SpringSecurityConfig {
|
||||
AuthenticationManager authenticationManager() {
|
||||
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
|
||||
daoAuthenticationProvider.setUserDetailsService(userDetailsService);
|
||||
daoAuthenticationProvider.setPasswordEncoder(securityBeanConfig.passwordEncoder());
|
||||
daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder());
|
||||
return new ProviderManager(daoAuthenticationProvider);
|
||||
}
|
||||
|
||||
@ -72,6 +82,43 @@ public class SpringSecurityConfig {
|
||||
.requestMatchers(HttpMethod.GET, "/api/rest/user/me","/api/rest/user/register","/api/rest/user/send-email").permitAll()
|
||||
.requestMatchers(HttpMethod.POST, "/api/rest/user/login","/api/rest/user/register").permitAll()
|
||||
.requestMatchers("/api/**").authenticated())
|
||||
// 不用注解,直接通过判断路径实现动态访问权限
|
||||
// .requestMatchers("/api/**").access((authentication, object) -> {
|
||||
// //表示请求的 URL 地址和数据库的地址是否匹配上了
|
||||
// boolean isMatch = false;
|
||||
// //获取当前请求的 URL 地址
|
||||
// String requestURI = object.getRequest().getRequestURI();
|
||||
// List<MenuWithRoleVO> menuWithRole = menuService.getMenuWithRole();
|
||||
// for (MenuWithRoleVO m : menuWithRole) {
|
||||
// AntPathMatcher antPathMatcher = new AntPathMatcher();
|
||||
// if (antPathMatcher.match(m.getUrl(), requestURI)) {
|
||||
// isMatch = true;
|
||||
// //说明找到了请求的地址了
|
||||
// //这就是当前请求需要的角色
|
||||
// List<Role> roles = m.getRoles();
|
||||
// //获取当前登录用户的角色
|
||||
// Collection<? extends GrantedAuthority> authorities = authentication.get().getAuthorities();
|
||||
// for (GrantedAuthority authority : authorities) {
|
||||
// for (Role role : roles) {
|
||||
// if (authority.getAuthority().equals(role.getName())) {
|
||||
// //说明当前登录用户具备当前请求所需要的角色
|
||||
// return new AuthorizationDecision(true);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// if (!isMatch) {
|
||||
// //说明请求的 URL 地址和数据库的地址没有匹配上,对于这种请求,统一只要登录就能访问
|
||||
// if (authentication.get() instanceof AnonymousAuthenticationToken) {
|
||||
// return new AuthorizationDecision(false);
|
||||
// } else {
|
||||
// //说明用户已经认证了
|
||||
// return new AuthorizationDecision(true);
|
||||
// }
|
||||
// }
|
||||
// return new AuthorizationDecision(false);
|
||||
// }))
|
||||
.addFilterAt(jsonAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
|
||||
.formLogin(form -> form
|
||||
.loginPage("/user/login")
|
||||
|
@ -60,7 +60,6 @@ public class UserDetailsImpl implements UserDetails {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
return authorities.stream().map(authority -> new SimpleGrantedAuthority(authority.getName())).collect(Collectors.toSet());
|
||||
}
|
||||
|
@ -106,14 +106,14 @@ public class UserController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询用户信息 hasAuthority('USER_QUERY')
|
||||
* 分页查询用户信息 hasAuthority('SYSTEM:USER:QUERY')
|
||||
*
|
||||
* @param query 查询表单
|
||||
* @param page 分页
|
||||
* @return 分页用户信息
|
||||
*/
|
||||
@GetMapping
|
||||
@PreAuthorize("hasAuthority('USER_QUERY·')")
|
||||
@PreAuthorize("hasAuthority('SYSTEM:USER:QUERY')")
|
||||
public Page<User> query(UserQuery query, Page<User> page) {
|
||||
return service.page(page, query.wrapper());
|
||||
}
|
||||
@ -121,58 +121,58 @@ public class UserController {
|
||||
|
||||
|
||||
/**
|
||||
* 新建用户 hasAuthority('USER_CREATE')
|
||||
* 新建用户 hasAuthority('SYSTEM:USER:CREATE')
|
||||
*
|
||||
* @param dto 表单数据
|
||||
* @return 新建的用户信息
|
||||
*/
|
||||
@PostMapping
|
||||
@PreAuthorize("hasAuthority('USER_CREATE')")
|
||||
@PreAuthorize("hasAuthority('SYSTEM:USER:CREATE')")
|
||||
public Boolean create(@RequestBody UserCreateDto dto) {
|
||||
return service.create(dto);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户 hasAuthority('USER_UPDATE')
|
||||
* 更新用户 hasAuthority('SYSTEM:USER:UPDATE')
|
||||
*
|
||||
* @param dto 表单数据
|
||||
* @param id ID
|
||||
* @return 更新后的用户
|
||||
*/
|
||||
@PatchMapping("{id}")
|
||||
@PreAuthorize("hasAuthority('USER_UPDATE')")
|
||||
@PreAuthorize("hasAuthority('SYSTEM:USER:UPDATE')")
|
||||
public Boolean update(@RequestBody UserUpdateDto dto, @PathVariable("id") Long id) {
|
||||
return service.update(dto, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户密码 hasAuthority('USER_UPDATE')
|
||||
* 更新用户密码 hasAuthority('SYSTEM:USER:UPDATE')
|
||||
*
|
||||
* @param id ID
|
||||
* @param password 新密码
|
||||
*/
|
||||
@PatchMapping("{id}/update-password")
|
||||
@PreAuthorize("hasAuthority('USER_UPDATE')")
|
||||
@PreAuthorize("hasAuthority('SYSTEM:USER:UPDATE')")
|
||||
public Boolean updatePassword(@PathVariable("id") Long id, @RequestParam String password) {
|
||||
return service.updatePassword(password, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换用户"启动/禁用"状态 hasAuthority('USER_DELETE')
|
||||
* 切换用户"启动/禁用"状态 hasAuthority('SYSTEM:USER:DELETE')
|
||||
*
|
||||
* @param id ID
|
||||
* @return Dept 更新后的用户
|
||||
*/
|
||||
@PatchMapping("{id}/toggle")
|
||||
@PreAuthorize("hasAuthority('USER_DELETE')")
|
||||
@PreAuthorize("hasAuthority('SYSTEM:USER:DELETE')")
|
||||
public Boolean toggle(@PathVariable("id") Long id) {
|
||||
return service.toggle(id);
|
||||
}
|
||||
/**
|
||||
* 删除用户 hasAuthority('USER_DELETE')
|
||||
* 删除用户 hasAuthority('SYSTEM:USER:DELETE')
|
||||
* */
|
||||
@DeleteMapping("{id}")
|
||||
@PreAuthorize("hasAuthority('USER_DELETE')")
|
||||
@PreAuthorize("hasAuthority('SYSTEM:USER:DELETE')")
|
||||
public Boolean delete(@PathVariable("id") Long id) {
|
||||
return service.removeById(id);
|
||||
}
|
||||
|
@ -1,10 +1,7 @@
|
||||
package com.zsc.edu.gateway.modules.system.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ -15,6 +12,7 @@ import java.util.Date;
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName("sys_authority")
|
||||
|
@ -1,9 +1,14 @@
|
||||
package com.zsc.edu.gateway.modules.system.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.EnumValue;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
|
||||
/**
|
||||
* 菜单
|
||||
@ -18,7 +23,12 @@ public class Menu extends BaseEntity {
|
||||
/**
|
||||
* 父菜单id
|
||||
*/
|
||||
@TableField(value = "pid", jdbcType = JdbcType.BIGINT)
|
||||
private Long pid;
|
||||
/**
|
||||
* 菜单类型
|
||||
*/
|
||||
private Type type = Type.PAGE;
|
||||
/**
|
||||
* 路由名称
|
||||
*/
|
||||
@ -52,8 +62,14 @@ public class Menu extends BaseEntity {
|
||||
*/
|
||||
private String permissions;
|
||||
|
||||
public Menu(Long pid, String name, String path, String locale, String icon, Boolean requiresAuth, Boolean hideInMenu, Integer order, String permissions) {
|
||||
/**
|
||||
* 权限字符串
|
||||
*/
|
||||
private String authority;
|
||||
|
||||
public Menu(Long pid, Type type, String name, String path, String locale, String icon, Boolean requiresAuth, Boolean hideInMenu, Integer order, String authority) {
|
||||
this.pid = pid;
|
||||
this.type = type;
|
||||
this.name = name;
|
||||
this.path = path;
|
||||
this.locale = locale;
|
||||
@ -61,6 +77,25 @@ public class Menu extends BaseEntity {
|
||||
this.requiresAuth = requiresAuth;
|
||||
this.hideInMenu = hideInMenu;
|
||||
this.menuOrder = order;
|
||||
this.permissions = permissions;
|
||||
this.authority = authority;
|
||||
}
|
||||
|
||||
/**
|
||||
* 菜单类型
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum Type {
|
||||
/**
|
||||
* 页面
|
||||
*/
|
||||
PAGE(0),
|
||||
/**
|
||||
* 按钮
|
||||
*/
|
||||
BUTTON(1);
|
||||
|
||||
@EnumValue
|
||||
private final int value;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.zsc.edu.gateway.modules.system.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* sys_role_menu
|
||||
* @author zhuang
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
@Setter
|
||||
@TableName("sys_role_menu")
|
||||
public class RoleMenu implements Serializable {
|
||||
/**
|
||||
* 角色ID
|
||||
*/
|
||||
private Long roleId;
|
||||
/**
|
||||
* 权限ID
|
||||
*/
|
||||
private Long menuId;
|
||||
|
||||
// @TableField(exist = false)
|
||||
// private Set<Authority> authorities;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.zsc.edu.gateway.modules.system.repo;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.zsc.edu.gateway.modules.system.entity.RoleMenu;
|
||||
|
||||
/**
|
||||
* @author Yao
|
||||
*/
|
||||
public interface RoleMenuRepository extends BaseMapper<RoleMenu> {
|
||||
|
||||
}
|
@ -69,7 +69,7 @@ class Meta {
|
||||
/**
|
||||
* 访问权限,使用","隔开的权限字符串
|
||||
*/
|
||||
private String permissions;
|
||||
private String[] permissions;
|
||||
|
||||
public Meta(Menu menu) {
|
||||
this.locale = menu.getLocale();
|
||||
@ -77,6 +77,6 @@ class Meta {
|
||||
this.requiresAuth = menu.getRequiresAuth();
|
||||
this.hideInMenu = menu.getHideInMenu();
|
||||
this.order = menu.getMenuOrder();
|
||||
this.permissions = menu.getPermissions();
|
||||
this.permissions = menu.getPermissions().split(",");
|
||||
}
|
||||
}
|
@ -3,9 +3,10 @@
|
||||
<mapper namespace="com.zsc.edu.gateway.modules.system.repo.UserRepository">
|
||||
|
||||
|
||||
<resultMap id="BaseResultMap1" type="com.zsc.edu.gateway.modules.system.vo.UserVo">
|
||||
<resultMap id="UserMap" type="com.zsc.edu.gateway.modules.system.entity.User">
|
||||
<id column="id" jdbcType="BIGINT" property="id"/>
|
||||
<result column="username" jdbcType="VARCHAR" property="username"/>
|
||||
<result column="password" jdbcType="VARCHAR" property="password"/>
|
||||
<result column="role_id" jdbcType="BIGINT" property="roleId"/>
|
||||
<result column="dept_id" jdbcType="BIGINT" property="deptId"/>
|
||||
<result column="email" jdbcType="VARCHAR" property="email"/>
|
||||
@ -14,27 +15,31 @@
|
||||
<result column="avatar" jdbcType="VARCHAR" property="avatar"/>
|
||||
<result column="address" jdbcType="VARCHAR" property="address"/>
|
||||
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
|
||||
<association property="role" column="role_id">
|
||||
<id column="id" jdbcType="BIGINT" property="id"/>
|
||||
<result column="name" property="name" />
|
||||
</association>
|
||||
</resultMap>
|
||||
<sql id="Base_Column_List">
|
||||
id, username, password, email, phone, create_time
|
||||
</sql>
|
||||
|
||||
<select id="selectByUsername" parameterType="java.lang.String" resultMap="BaseResultMap1">
|
||||
<select id="selectByUsername" parameterType="java.lang.String" resultMap="UserMap">
|
||||
select
|
||||
*
|
||||
from sys_user
|
||||
left join study.sys_role sr on sys_user.role_id = sr.id
|
||||
left join sys_role sr on sys_user.role_id = sr.id
|
||||
where username = #{username,jdbcType=VARCHAR}
|
||||
</select>
|
||||
|
||||
<select id="page" resultType="com.zsc.edu.gateway.modules.system.vo.UserVo" resultMap="BaseResultMap1">
|
||||
<select id="page" resultType="com.zsc.edu.gateway.modules.system.vo.UserVo" resultMap="UserMap">
|
||||
select
|
||||
*
|
||||
from sys_user
|
||||
${ew.customSqlSegment}
|
||||
</select>
|
||||
|
||||
<select id="detail" resultType="com.zsc.edu.gateway.modules.system.vo.UserVo" resultMap="BaseResultMap1">
|
||||
<select id="detail" resultType="com.zsc.edu.gateway.modules.system.vo.UserVo" resultMap="UserMap">
|
||||
select
|
||||
*
|
||||
from sys_user
|
||||
|
@ -1,37 +1,70 @@
|
||||
package com.zsc.edu.gateway.modules.system.service.impl;
|
||||
|
||||
import com.zsc.edu.gateway.modules.system.entity.Menu;
|
||||
import com.zsc.edu.gateway.modules.system.entity.Role;
|
||||
import com.zsc.edu.gateway.modules.system.entity.RoleMenu;
|
||||
import com.zsc.edu.gateway.modules.system.repo.RoleMenuRepository;
|
||||
import com.zsc.edu.gateway.modules.system.service.MenuService;
|
||||
import com.zsc.edu.gateway.modules.system.service.RoleService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
class MenuServiceImplTest {
|
||||
|
||||
@Autowired
|
||||
private MenuService menuService;
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
@Autowired
|
||||
private RoleMenuRepository roleMenuRepository;
|
||||
|
||||
@Test
|
||||
public void insertRootMenu() {
|
||||
Menu dashboard = new Menu(null, "Dashboard", "/dashboard", "dashboard", "icon-dashboard", true, false, 1, "*");
|
||||
Menu system = new Menu(null, "System", "/system", "menu.system", "icon-computer", true, false, 2, "*");
|
||||
Menu user = new Menu(null, "User", "/user", "menu.user", "icon-user", true, false, 3, "*");
|
||||
Menu message = new Menu(null, "Message", "/message", "消息管理", "icon-message", true, false, 4, "*");
|
||||
Menu dashboard = new Menu(null, Menu.Type.PAGE, "Dashboard", "/dashboard", "dashboard", "icon-dashboard", true, false, 1, "dashboard");
|
||||
Menu system = new Menu(null, Menu.Type.PAGE, "System", "/system", "menu.system", "icon-computer", true, false, 2, "system");
|
||||
Menu user = new Menu(null, Menu.Type.PAGE, "User", "/user", "menu.user", "icon-user", true, false, 3, "user");
|
||||
Menu message = new Menu(null, Menu.Type.PAGE, "Message", "/message", "消息管理", "icon-message", true, false, 4, "message");
|
||||
menuService.saveBatch(List.of(dashboard, system, user, message));
|
||||
Menu workplace = new Menu(dashboard.getId(), "Workplace", "workplace", "工作台", "icon-dashboard", true, false, 1, "*");
|
||||
Menu role = new Menu(system.getId(), "Role", "role", "角色管理", null, true, false, 1, "*");
|
||||
Menu dept = new Menu(system.getId(), "Dept", "dept", "部门管理", null, true, false, 2, "*");
|
||||
Menu users = new Menu(system.getId(), "User", "user", "用户管理", null, true, false, 3, "*");
|
||||
Menu authority = new Menu(system.getId(), "Authority", "authority", "权限管理", null, true, false, 4, "*");
|
||||
Menu menu = new Menu(system.getId(), "Menu", "menu", "菜单管理", null, true, false, 5, "*");
|
||||
Menu notice = new Menu(message.getId(), "Notice", "notice", "通知管理", null, true, false, 1, "*");
|
||||
Menu bulletin = new Menu(message.getId(), "Bulletin", "bulletin", "公告管理", null, true, false, 2, "*");
|
||||
menuService.saveBatch(List.of(workplace, role, dept, users, authority, menu, notice, bulletin));
|
||||
Menu workplace = new Menu(dashboard.getId(), Menu.Type.PAGE, "Workplace", "workplace", "工作台", "icon-dashboard", true, false, 1, "dashboard:workplace");
|
||||
Menu role = new Menu(system.getId(), Menu.Type.PAGE, "Role", "role", "角色管理", null, true, false, 1, "system:role");
|
||||
Menu roleDelete = new Menu(role.getId(), Menu.Type.BUTTON, "roleDelete", null, "角色删除", null, true, false, 1, "system:role:delete");
|
||||
Menu dept = new Menu(system.getId(), Menu.Type.PAGE, "Dept", "dept", "部门管理", null, true, false, 2, "system:dept");
|
||||
Menu users = new Menu(system.getId(), Menu.Type.PAGE, "User", "user", "用户管理", null, true, false, 3, "system:user");
|
||||
Menu authority = new Menu(system.getId(), Menu.Type.BUTTON, "Authority", "authority", "权限管理", null, true, false, 4, "system:authority");
|
||||
Menu menu = new Menu(system.getId(), Menu.Type.PAGE, "Menu", "menu", "菜单管理", null, true, false, 5, "system:menu");
|
||||
Menu notice = new Menu(message.getId(), Menu.Type.PAGE, "Notice", "notice", "通知管理", null, true, false, 1, "message:notice");
|
||||
Menu bulletin = new Menu(message.getId(), Menu.Type.PAGE, "Bulletin", "bulletin", "公告管理", null, true, false, 2, "message:bulletin");
|
||||
menuService.saveBatch(List.of(workplace, roleDelete, dept, users, authority, menu, notice, bulletin));
|
||||
Menu roleCreate = new Menu(role.getId(), Menu.Type.BUTTON, "roleCreate", null, "角色新增", null, true, false, 1, "system:role:create");
|
||||
Menu roleUpdate = new Menu(role.getId(), Menu.Type.BUTTON, "roleUpdate", null, "角色修改", null, true, false, 1, "system:role:update");
|
||||
Menu roleQuery = new Menu(role.getId(), Menu.Type.BUTTON, "roleQuery", null, "角色查询", null, true, false, 1, "system:role:query");
|
||||
Menu deptSave = new Menu(dept.getId(), Menu.Type.BUTTON, "deptCreate", null, "部门新增", null, true, false, 1, "system:dept:create");
|
||||
Menu deptUpdate = new Menu(dept.getId(), Menu.Type.BUTTON, "deptUpdate", null, "部门修改", null, true, false, 1, "system:dept:update");
|
||||
Menu deptQuery = new Menu(dept.getId(), Menu.Type.BUTTON, "deptQuery", null, "部门查询", null, true, false, 1, "system:dept:query");
|
||||
Menu deptDelete = new Menu(dept.getId(), Menu.Type.BUTTON, "deptDelete", null, "部门删除", null, true, false, 1, "system:dept:delete");
|
||||
Menu userSave = new Menu(dept.getId(), Menu.Type.BUTTON, "userCreate", null, "用户新增", null, true, false, 1, "system:user:create");
|
||||
Menu userUpdate = new Menu(dept.getId(), Menu.Type.BUTTON, "userUpdate", null, "用户修改", null, true, false, 1, "system:user:update");
|
||||
Menu userQuery = new Menu(dept.getId(), Menu.Type.BUTTON, "userQuery", null, "用户查询", null, true, false, 1, "system:user:query");
|
||||
Menu userDelete = new Menu(dept.getId(), Menu.Type.BUTTON, "userDelete", null, "用户删除", null, true, false, 1, "system:user:delete");
|
||||
Menu menuSave = new Menu(menu.getId(), Menu.Type.BUTTON, "menuCreate", null, "菜单新增", null, true, false, 1, "system:menu:create");
|
||||
Menu menuUpdate = new Menu(menu.getId(), Menu.Type.BUTTON, "menuUpdate", null, "菜单修改", null, true, false, 1, "system:menu:update");
|
||||
Menu menuQuery = new Menu(menu.getId(), Menu.Type.BUTTON, "menuQuery", null, "菜单查询", null, true, false, 1, "system:menu:query");
|
||||
Menu menuDelete = new Menu(menu.getId(), Menu.Type.BUTTON, "menuDelete", null, "菜单删除", null, true, false, 1, "system:menu:delete");
|
||||
menuService.saveBatch(List.of(roleCreate, roleUpdate, roleQuery, deptSave, deptUpdate, deptQuery, deptDelete, userSave, userUpdate, userQuery, userDelete, menuSave, menuUpdate, menuQuery, menuDelete));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Role admin = roleService.lambdaQuery().eq(Role::getName, "admin").one();
|
||||
menuService.list().forEach(menu -> {
|
||||
roleMenuRepository.insert(new RoleMenu(admin.getId(), menu.getId()));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -9,6 +9,7 @@ import com.zsc.edu.gateway.modules.system.repo.UserRepository;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -27,6 +28,9 @@ public class UserServiceTest {
|
||||
private UserRepository userRepository;
|
||||
@Resource
|
||||
private AuthorityRepository authorityRepository;
|
||||
|
||||
@Resource
|
||||
private PasswordEncoder passwordEncoder;
|
||||
@Test
|
||||
void test() {
|
||||
User user=userRepository.selectByUsername("admin");
|
||||
@ -37,8 +41,9 @@ public class UserServiceTest {
|
||||
}
|
||||
@Test
|
||||
void test1() {
|
||||
User user=userRepository.selectByUsername("admin");
|
||||
System.out.println(user);
|
||||
|
||||
// User user=userRepository.selectByUsername("admin");
|
||||
// System.out.println(user);
|
||||
String admin = passwordEncoder.encode("admin");
|
||||
System.out.println(admin);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user