日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringtBoot+SpringSecurity+Jwt+MyBatis整合实现用户认证以及权限控制

發布時間:2025/3/21 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringtBoot+SpringSecurity+Jwt+MyBatis整合实现用户认证以及权限控制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 前言
    • 數據庫表結構
    • 項目結構圖
    • 核心配置類SecurityConfig
    • 實體類
    • 工具類
    • 用戶登錄認證
    • Token令牌驗證
    • 獲取用戶權限
    • 用戶權限驗證
    • Service層實現類
    • 統一響應類
    • Controller
    • 流程解析
    • 測試
      • 登錄測試
      • 公共接口測試
      • 訪問具有訪問權限的接口
      • 訪問無訪問權限的接口

前言

本項目采用SpringBoot+SpringSecurity+Jwt+Mybatis,實現了基于數據庫的用戶認證以及權限分配。

pom.xml導入依賴

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-test</artifactId><scope>test</scope></dependency><!--自定義的處理類中需要用到--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.73</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>0.9.1</version></dependency><!--注解配置處理器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.6</version></dependency><!--Swagger相關依賴--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency></dependencies>

數據庫表結構

/*Navicat Premium Data TransferSource Server : Taylor SwiftSource Server Type : MySQLSource Server Version : 80023Source Host : localhost:3306Source Schema : securityTarget Server Type : MySQLTarget Server Version : 80023File Encoding : 65001Date: 18/10/2021 18:20:16 */SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0;-- ---------------------------- -- Table structure for menu -- ---------------------------- DROP TABLE IF EXISTS `menu`; CREATE TABLE `menu` (`id` int NOT NULL AUTO_INCREMENT,`pattern` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '請求路徑匹配規則',PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;-- ---------------------------- -- Records of menu -- ---------------------------- INSERT INTO `menu` VALUES (1, '/db/**'); INSERT INTO `menu` VALUES (2, '/admin/**'); INSERT INTO `menu` VALUES (3, '/user/**');-- ---------------------------- -- Table structure for menu_role -- ---------------------------- DROP TABLE IF EXISTS `menu_role`; CREATE TABLE `menu_role` (`id` int NOT NULL AUTO_INCREMENT,`mid` int NOT NULL COMMENT 'menu表外鍵',`rid` int NOT NULL COMMENT 'role表外鍵',PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;-- ---------------------------- -- Records of menu_role -- ---------------------------- INSERT INTO `menu_role` VALUES (1, 1, 1); INSERT INTO `menu_role` VALUES (2, 2, 2); INSERT INTO `menu_role` VALUES (3, 3, 3);-- ---------------------------- -- Table structure for role -- ---------------------------- DROP TABLE IF EXISTS `role`; CREATE TABLE `role` (`id` int NOT NULL AUTO_INCREMENT,`name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`nameZh` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;-- ---------------------------- -- Records of role -- ---------------------------- INSERT INTO `role` VALUES (1, 'dba', '數據庫管理員'); INSERT INTO `role` VALUES (2, 'admin', '系統管理員'); INSERT INTO `role` VALUES (3, 'user', '用戶');-- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` (`id` int NOT NULL AUTO_INCREMENT,`username` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`enabled` tinyint(1) NULL DEFAULT NULL,`locked` tinyint(1) NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;-- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, 'root', '$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq', 1, 0); INSERT INTO `user` VALUES (2, 'admin', '$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq', 1, 0); INSERT INTO `user` VALUES (3, 'user', '$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq', 1, 0); INSERT INTO `user` VALUES (4, 'Sabrina', '$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq', 1, 1);-- ---------------------------- -- Table structure for user_role -- ---------------------------- DROP TABLE IF EXISTS `user_role`; CREATE TABLE `user_role` (`id` int NOT NULL AUTO_INCREMENT,`uid` int NULL DEFAULT NULL,`rid` int NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;-- ---------------------------- -- Records of user_role -- ---------------------------- INSERT INTO `user_role` VALUES (1, 1, 1); INSERT INTO `user_role` VALUES (2, 1, 2); INSERT INTO `user_role` VALUES (3, 2, 2); INSERT INTO `user_role` VALUES (4, 3, 3);SET FOREIGN_KEY_CHECKS = 1;

項目結構圖


核心配置類SecurityConfig

@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {@AutowiredJwtUserDetailService userService;@AutowiredPermissionFilter permissionFilter;@AutowiredUserAccessDecisionManager userAccessDecisionManager;@BeanPermissionFilter permissionFilter(){return new PermissionFilter();}@BeanPasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception{auth.userDetailsService(userService).passwordEncoder(passwordEncoder());}@Overrideprotected void configure(HttpSecurity http) throws Exception{http.authorizeRequests()//注冊PermissionFilter和UserAccessDecisionManager進行權限管理.withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>(){@Overridepublic <O extends FilterSecurityInterceptor> O postProcess(O o){o.setAccessDecisionManager(userAccessDecisionManager);o.setSecurityMetadataSource(permissionFilter);return o;}})// 放行靜態資源.antMatchers(HttpMethod.GET,"/*.html","/**/*.html","/**/*.css","/**/*.js","/webSocket/**").permitAll()// 放行文件訪問.antMatchers("/file/**").permitAll()// 放行druid.antMatchers("/druid/**").permitAll()// 放行OPTIONS請求.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()//配置登錄路徑.antMatchers(HttpMethod.POST,"/login").permitAll()//其他請求都需要進行認證.anyRequest().authenticated().and()//添加登錄過濾器,當請求為"login"時該過濾器截取請求.addFilterBefore(new JwtLoginFilter("/login",authenticationManager()), UsernamePasswordAuthenticationFilter.class)//添加token校驗過濾器,每次發起請求都需要通過該過濾器進行判斷是否處于登錄狀態.addFilterBefore(new JwtTokenFilter(),UsernamePasswordAuthenticationFilter.class)//解決跨域問題.cors().and().csrf().disable();}@Overridepublic void configure(WebSecurity web) throws Exception{web.ignoring().antMatchers("/swagger-ui.html","v2/api-docs").antMatchers("/v2/**").antMatchers("/swagger-resources/**").antMatchers("/webjars/**").antMatchers("/*/api-docs").antMatchers("/doc.html");} }

實體類

public class JwtUserDetails implements UserDetails {private Integer id;private String username;private String password;private Boolean enabled;private Boolean locked;private List<Role> roles;private String token;@Overridepublic Collection<? extends GrantedAuthority> getAuthorities(){return roles == null ? new ArrayList<SimpleGrantedAuthority>(): roles.stream().map(role -> new SimpleGrantedAuthority(String.valueOf(role.getRoleId()))).collect(Collectors.toList());}@Overridepublic String getPassword(){return this.password;}@Overridepublic String getUsername(){return this.password;}@Overridepublic boolean isAccountNonExpired(){return true;}@Overridepublic boolean isAccountNonLocked(){return !locked;}@Overridepublic boolean isCredentialsNonExpired(){return true;}@Overridepublic boolean isEnabled(){return enabled;}public Integer getId(){return id;}public void setId(Integer id){this.id = id;}public Boolean getEnabled(){return enabled;}public void setEnabled(Boolean enabled){this.enabled = enabled;}public Boolean getLocked(){return locked;}public void setLocked(Boolean locked){this.locked = locked;}public String getToken(){return token;}public void setToken(String token){this.token = token;}public List<Role> getRoles(){return roles;}public void setRoles(List<Role> roles){this.roles = roles;}} @NoArgsConstructor @AllArgsConstructor @Data public class Menu {private Integer id;/*** 訪問路徑*/private String pattern;/*** 訪問當前路徑所需要的角色*/private List<Role> roles; } @Data @AllArgsConstructor @NoArgsConstructor public class Role {private Integer roleId;private String name;/*** 角色中文名*/private String nameZh; }

工具類

@Component @Slf4j public class JwtTokenUtil {public final static String CLAIMS_KEY_AUTHORITIES = "authorities";public final static String SECRET = "Turing-Team";public final static Long TOKEN_VALIDITY_IN_SECONDS = 600000000L;public void printLogger(Exception e){log.error("Error:{}",e.getMessage());}public Claims getClaimsFromToken(String token){Claims claims;try{claims = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();}catch (ExpiredJwtException e){printLogger(e);claims = null;}catch (IncorrectClaimException e){printLogger(e);claims = null;}catch (Exception e){printLogger(e);claims = null;}return claims;}public String getUsernameFromToken(String token){String username;try{Claims claims = getClaimsFromToken(token);username = claims.getSubject();}catch (Exception e){printLogger(e);username = null;}return username;}public Date getCreateDateFromToken(String token){Date date;try{Claims claims = getClaimsFromToken(token);date = claims.getIssuedAt();}catch (Exception e){date = null;printLogger(e);}return date;}public Date getExpirationDateFromToken(String token){Date date;try{Claims claims = getClaimsFromToken(token);date = claims.getExpiration();}catch (Exception e){printLogger(e);date = null;}return date;}private Date generateExpirationDate(){return new Date(System.currentTimeMillis() + TOKEN_VALIDITY_IN_SECONDS);}public boolean isTokenExpired(String token){Date date = getExpirationDateFromToken(token);return getClaimsFromToken(token) == null || date.before(new Date());}public String generateToken(String username,StringBuffer roles){Claims claims = new DefaultClaims();claims.setSubject(username).setIssuedAt(new Date()).setExpiration(generateExpirationDate());return Jwts.builder().setClaims(claims).claim(CLAIMS_KEY_AUTHORITIES,roles).signWith(SignatureAlgorithm.HS512, SECRET).compact();}public String refreshToken(String token){String refreshedToken;try{Claims claims = getClaimsFromToken(token);StringBuffer roles = (StringBuffer) claims.get(CLAIMS_KEY_AUTHORITIES);refreshedToken = generateToken(claims.getSubject(),roles);}catch (Exception e){printLogger(e);refreshedToken = null;}return refreshedToken;}public boolean validateToken(String token, UserDetails userDetails){JwtUserDetails user = (JwtUserDetails) userDetails;String username = getUsernameFromToken(token);return username.equals(user.getUsername()) && !isTokenExpired(token);}public Jws<Claims> parserToken(String token)throws JwtException{return Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token);}} @Component public class SpringInjectUtil implements ApplicationContextAware {private static ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {if(SpringInjectUtil.applicationContext == null){SpringInjectUtil.applicationContext = applicationContext;}}public static ApplicationContext getApplicationContext(){return applicationContext;}//根據namepublic static Object getBean(String name){return getApplicationContext().getBean(name);}//根據類型public static <T> T getBean(Class<T> clazz){return getApplicationContext().getBean(clazz);}public static <T> T getBean(String name,Class<T> clazz){return getApplicationContext().getBean(name,clazz);}} @Data @Configuration() @ConfigurationProperties(prefix = "jwt") //從application.yaml配置中獲取 public class JwtSecurityProperties {private String header;private String secret;private Long tokenValidityInSeconds; } #Jwt Configuration jwt:header: Authorizationsecret: Turing-Teamtoken_validity-in-seconds: 600000000

用戶登錄認證

public class JwtLoginFilter extends AbstractAuthenticationProcessingFilter {@ResourceJwtTokenUtil jwtTokenUtil;private void init(){if (jwtTokenUtil == null){jwtTokenUtil = (JwtTokenUtil) SpringInjectUtil.getBean("jwtTokenUtil");}}public JwtLoginFilter(String defaultFilterProcessesUrl, AuthenticationManager authenticationManager){super(new AntPathRequestMatcher(defaultFilterProcessesUrl));init();setAuthenticationManager(authenticationManager);}@Overridepublic Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException{//從請求中獲取用戶信息String username = httpServletRequest.getParameter("username");String password = httpServletRequest.getParameter("password");UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);//認證->會調用JwtUserDetailService.loadUserByUsernamereturn getAuthenticationManager().authenticate(token);}@Overrideprotected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException{response.setContentType("application/json;charset=utf-8");Result<JwtUserDetails> result = new Result<>();PrintWriter out = response.getWriter();String responseResult;Collection<? extends GrantedAuthority> authorities = authResult.getAuthorities();StringBuffer roles = new StringBuffer();String username = request.getParameter("username");JwtUserDetails user = (JwtUserDetails) authResult.getPrincipal();//遍歷用戶角色,將其寫入token,角色之間用逗號隔開List<Role> rolesList = user.getRoles();for (Role role : rolesList){if (role != null){System.out.println(role);roles.append(role.getRoleId()+",");}}//生成tokenString token = jwtTokenUtil.generateToken(username, roles);user.setToken(token);result.code(ResultCode.SUCCESS).message("Login Success").data(user);//將封裝好的帶有token的用戶信息返回前端out.write(new ObjectMapper().writeValueAsString(result));out.flush();out.close();}@Overrideprotected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException{response.setContentType("application/json;charset=utf-8");PrintWriter out =response.getWriter();Result<String> result = new Result<>();//賬戶是否過期/被鎖定/不可用由SpringSecurity進行檢查之后報出異常//AbstractUserDetailsAuthenticationProvider.check()中進行檢查if (failed instanceof LockedException){result.code(ResultCode.ERROR).message("賬號已被鎖定,無法進行登錄操作!").data("Account had been locked");}else if (failed instanceof DisabledException){result.code(ResultCode.ERROR).message("賬號不可用,無法進行登錄操作!").data("Account is disabled");}else{result.message("用戶名或者密碼錯誤,請重新登錄!").code(ResultCode.ERROR).data("Login Failure");}out.write(new ObjectMapper().writeValueAsString(result));out.flush();out.close();} }

Token令牌驗證

public class JwtTokenFilter extends GenericFilterBean {private static JwtTokenFilter jwtTokenFilter;@ResourceJwtSecurityProperties jwtSecurityProperties;@ResourceJwtTokenUtil jwtTokenUtil;private void init(){if (jwtSecurityProperties == null){jwtSecurityProperties = (JwtSecurityProperties) SpringInjectUtil.getBean("jwtSecurityProperties");}if (jwtTokenUtil == null){jwtTokenUtil = (JwtTokenUtil) SpringInjectUtil.getBean("jwtTokenUtil");}}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException{init();HttpServletRequest request = (HttpServletRequest) servletRequest;HttpServletResponse response = (HttpServletResponse) servletResponse;//從請求頭中獲取tokenString token = request.getHeader(jwtSecurityProperties.getHeader());PrintWriter out;Result result;response.setContentType("application/json;charset=utf-8");if ("".equals(token) || token == null){out = response.getWriter();result = new Result<String>();result.code(ResultCode.UNAUTHORIZED).message("必須攜帶用戶的認證信息進行訪問").data("User Unauthorized");out.write(new ObjectMapper().writeValueAsString(result));out.flush();out.close();return;}if (jwtTokenUtil.isTokenExpired(token)){out = response.getWriter();result = new Result<String>();result.code(ResultCode.UNAUTHORIZED).message("登錄認證已過期,請重新登錄!").data("User Authorization Is Expired");out.write(new ObjectMapper().writeValueAsString(result));out.flush();out.close();return;}Jws<Claims> jws;try{jws = jwtTokenUtil.parserToken(token);}catch (JwtException e){out = response.getWriter();result = new Result<String>();result.code(ResultCode.UNAUTHORIZED).message("登錄認證無效!").data("Invalid Authorization ");out.write(new ObjectMapper().writeValueAsString(result));out.flush();out.close();return;}Claims user = jws.getBody();//獲取用戶名String username = user.getSubject();//獲取用戶角色,以逗號作分割的字符串String authoritiesString = (String) user.get(JwtTokenUtil.CLAIMS_KEY_AUTHORITIES);//自動轉化為GrantedAuthority對象List<GrantedAuthority> authorities =AuthorityUtils.commaSeparatedStringToAuthorityList(authoritiesString);//對用戶信息進行校驗UsernamePasswordAuthenticationToken authenticationToken =new UsernamePasswordAuthenticationToken(username, null, authorities);SecurityContextHolder.getContext().setAuthentication(authenticationToken);//放行filterChain.doFilter(servletRequest, servletResponse);} }

獲取用戶權限

public class PermissionFilter implements FilterInvocationSecurityMetadataSource {/*** 路徑匹配類,用于檢查用戶的請求路徑是否與數據庫中的對應*/AntPathMatcher antPathMatcher = new AntPathMatcher();@AutowiredMenuService menuService;private void init(){if (menuService == null){menuService = (MenuService) SpringInjectUtil.getBean("menuService");}}/*** 每次用戶發送請求都會先進入此方法,分析出該請求地址需要哪些角色權限* @param o* @return* @throws IllegalArgumentException*/@Overridepublic Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException{init();FilterInvocation filterInvocation = (FilterInvocation) o;//獲取請求地址String requestUrl = filterInvocation.getRequestUrl();List<Menu> menus = menuService.findAllMenusWithRoles();//遍歷所有訪問路徑規則for (Menu menu : menus){if (antPathMatcher.match(menu.getPattern(),requestUrl)){List<Role> roles = menu.getRoles();String[] rolesArray = new String[roles.size()];for (int i = 0; i < roles.size(); i++){rolesArray[i] = String.valueOf(roles.get(i).getRoleId());}return SecurityConfig.createList(rolesArray);}}//所有的路徑都匹配不上,返回默認的標識符,表示該路徑是登錄即可訪問的return SecurityConfig.createList("Login-Common");}@Overridepublic Collection<ConfigAttribute> getAllConfigAttributes(){return null;}/*** 是否支持該方式,返回true* @param aClass* @return*/@Overridepublic boolean supports(Class<?> aClass){return true;} }

用戶權限驗證

@Component public class UserAccessDecisionManager implements AccessDecisionManager {/*** 判斷用戶是否有訪問該路徑的權限* @param authentication 可以獲取用戶信息* @param o 實際上是FilterInvocation對象,可以獲取請求路徑* @param collection 訪問該路徑所需要的角色列表* @throws AccessDeniedException 非法訪問異常* @throws InsufficientAuthenticationException*/@Overridepublic void decide(Authentication authentication, Object o, Collection<ConfigAttribute> collection) throws AccessDeniedException, InsufficientAuthenticationException{//遍歷訪問該路徑所需要的所有角色名for (ConfigAttribute configAttribute : collection){//如果是登錄后即可訪問if ("Login-Common".equals(configAttribute.getAttribute())){if (authentication instanceof AnonymousAuthenticationToken){throw new AccessDeniedException("尚未登陸,請前往登錄!");}return;}//獲取當前用戶的具有的角色Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();//遍歷該用戶的角色并判斷該角色是否有權限訪問當前請求路徑for (GrantedAuthority authority : authorities){//將用戶的角色信息與訪問該路徑所需要角色進行匹配if (authority.getAuthority().equals(configAttribute.getAttribute())){return;}}}throw new AccessDeniedException("權限不足,請聯系管理員!");}@Overridepublic boolean supports(ConfigAttribute configAttribute){return true;}@Overridepublic boolean supports(Class<?> aClass){return true;} }

Service層實現類

@Service public class JwtUserDetailService implements UserDetailsService {@Autowiredprivate UserMapper userMapper;@Autowiredprivate RoleMapper roleMapper;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{JwtUserDetails jwtUserDetails = userMapper.loadUserByUsername(username);if (jwtUserDetails == null){throw new UsernameNotFoundException("用戶不存在");}jwtUserDetails.setRoles(roleMapper.findRolesByUserId(jwtUserDetails.getId()));return jwtUserDetails;}} @Service public class MenuServiceImpl implements MenuService {@AutowiredMenuMapper menuMapper;@Overridepublic List<Menu> findAllMenusWithRoles(){return menuMapper.findAllMenuWithRoles();}} @Service public class RoleServiceImpl implements RoleService {@AutowiredJwtUserDetailService userDetailService;@AutowiredRoleMapper roleMapper;@Overridepublic Role findRolesByUsername(String username){JwtUserDetails user = (JwtUserDetails) userDetailService.loadUserByUsername(username);return roleMapper.findRoleByUserId(user.getId());} }

統一響應類

public class Result<T> {private Integer code;private String message;private T data;public Result() {}public Result message(String message){this.message = message;return this;}public Result code(Integer code){this.code = code;return this;}public Result data(T data){this.data = data;return this;}}

Controller

@RestController @RequestMapping public class UserController {@GetMapping("/hello")public Result hello(){return new Result().message("訪問公共接口成功").code(ResultCode.SUCCESS).data("Success");}@GetMapping("/db/hello")public Result db(){return new Result().message("訪問dba角色接口成功").code(ResultCode.SUCCESS).data("Success");}@GetMapping("/admin/hello")public Result admin(){return new Result().message("訪問admin接口成功").code(ResultCode.SUCCESS).data("Success");}@GetMapping("/user/hello")public Result user(){return new Result().message("訪問user接口成功").code(ResultCode.SUCCESS).data("Success");}}

流程解析

  • 用戶發起登錄請求之后,會被JwtLoginFilter所攔截,進行登錄信息驗證;登錄成功之后會返回用戶信息,并且讓用戶的之后發送來的請求頭中攜帶生成的Token。
  • 用戶發起非登錄請求之后,會被JwtTokenFilter所攔截,解析并驗證請求中所帶的Token,判斷是否處于已登錄狀態。
  • 已登陸的情況下,會繼續被PermissionFilter所攔截,判斷用戶此次訪問請求需要具備哪些角色,然后將其封裝起來發送至請求訪問管理類UserAccessDecisionManager。
  • 請求訪問管理類UserAccessDecisionManager得到信息后,判斷此次訪問是否合法,如果合法則放行,否則將拋出異常。

測試

登錄測試

登錄成功:

賬戶被鎖定:

賬戶不可用:

賬戶不存在:


公共接口測試

訪問具有訪問權限的接口

數據庫管理員 :

管理員:

訪問無訪問權限的接口

用戶:

以上,整個登錄認證以及權限控制功能已經搭建好,可以在此基礎上根據需求添加其他的業務。

該文僅作為本人學習使用,水平與能力有限,如有錯誤或不足歡迎指正!

總結

以上是生活随笔為你收集整理的SpringtBoot+SpringSecurity+Jwt+MyBatis整合实现用户认证以及权限控制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。