当前位置:   article > 正文

springboot基于数据库的认证Spring Security_spring boot+spring security实现用户的登录登出配置基于数据库的认证信息和角

spring boot+spring security实现用户的登录登出配置基于数据库的认证信息和角色

Spring Boot针对Spring Security提供了自动化配置方案,因此可以使Spring Security非常容易地整合进Spring Boot项目中,这也是在Spring Boot项目中使用Spring Security的优势。

基本用法

创建项目,添加依赖

创建一个Spring Boot Web项目,然后添加spring-boot-starter-security依赖即可,代码如下:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>

只要开发者在项目中添加了spring-boot-starter-security依赖,项目中所有资源都会被保护起来。

添加hello接口

接下来在项目中添加一个简单的/hello接口,内容如下:

  1. package com.shrimpking.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * Created by IntelliJ IDEA.
  6. *
  7. * @Author : Shrimpking
  8. * @create 2023/6/6 17:22
  9. */
  10. @RestController
  11. public class HelloController
  12. {
  13. @GetMapping("/hello")
  14. public String hello()
  15. {
  16. return "hello";
  17. }
  18. }

配置用户名和密码

如果开发者对默认的用户名和密码不满意,可以在application.properties中配置默认的用户名、密码以及用户角色,配置方式如下:

  1. server.port=8099
  2. spring.security.user.name=user
  3. spring.security.user.password=1234
  4. spring.security.user.roles=admin

启动项目测试

接下来启动项目,启动成功后,访问/hello接口会自动跳转到登录页面,这个登录页面是由Spring Security提供的,如图所示。

 

 基于数据库的认证

 在真实项目中,用户的基本信息以及角色等都存储在数据库中,因此需要从数据库中获取数据进行认证。介绍如何使用数据库中的数据进行认证和授权。

设计数据表

首先需要设计一个基本的用户角色表,一共三张表,分别是用户表、角色表以及用户角色关联表。为了方便测试,预置几条测试数据

  1. drop table if EXISTS r_user;
  2. create table r_user(
  3. id int(11) not null auto_increment primary key,
  4. userName varchar(32) not null,
  5. password varchar(50) not null,
  6. enabled tinyint default 1 comment '是否启用,0未启用,1启用',
  7. locked tinyint default 0 comment '是否锁定,0锁定,1未锁定'
  8. ) comment '用户表';
  9. insert into r_user(userName,password) values ('root','1234');
  10. insert into r_user(userName,password) values ('admin','1234');
  11. insert into r_user(userName,password) values ('king','1234');
  12. drop table if EXISTS r_role;
  13. create table r_role(
  14. id int(11) not null auto_increment,
  15. name varchar(32) not null comment '角色英文名称',
  16. displayName varchar(32) not null comment '角色中文名称',
  17. primary key(id)
  18. ) comment '角色表';
  19. insert into r_role(name,displayName) values('ROLE_dba','数据库管理员');
  20. insert into r_role(name,displayName) values('ROLE_admin','系统管理员');
  21. insert into r_role(name,displayName) values('ROLE_user','用户');
  22. drop table if EXISTS r_user_role;
  23. create table r_user_role(
  24. id int(11) not null auto_increment primary key,
  25. userId int not null comment '用户id',
  26. roleId int not null comment '角色id'
  27. ) comment '用户角色关联表';
  28. insert into r_user_role(userId,roleId) values (1,1);
  29. insert into r_user_role(userId,roleId) values (1,2);
  30. insert into r_user_role(userId,roleId) values (1,3);
  31. insert into r_user_role(userId,roleId) values (2,2);
  32. insert into r_user_role(userId,roleId) values (2,3);
  33. insert into r_user_role(userId,roleId) values (3,3);

创建项目,选择MyBatis,因此创建Spring Boot Web项目添加如下依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>
  9. <dependency>
  10. <groupId>org.mybatis.spring.boot</groupId>
  11. <artifactId>mybatis-spring-boot-starter</artifactId>
  12. <version>1.3.2</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>mysql</groupId>
  16. <artifactId>mysql-connector-java</artifactId>
  17. <version>5.1.47</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>com.alibaba</groupId>
  21. <artifactId>druid</artifactId>
  22. <version>1.1.10</version>
  23. </dependency>

  

配置数据库

 在application.properties中进行数据库连接配置:

  1. server.port=8099
  2. #数据源
  3. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  4. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  5. spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimeZone=UTC
  6. spring.datasource.username=root
  7. spring.datasource.password=mysql123

创建实体类

分别创建角色表和用户表对应的实体类,代码如下:

Role.java

  1. package com.shrimpking.pojo;
  2. /**
  3. * Created by IntelliJ IDEA.
  4. *
  5. * @Author : Shrimpking
  6. * @create 2023/6/6 18:27
  7. */
  8. public class Role
  9. {
  10. private String id;
  11. private String name;
  12. private String displayName;
  13. public Role()
  14. {
  15. }
  16. public Role(String name, String displayName)
  17. {
  18. this.name = name;
  19. this.displayName = displayName;
  20. }
  21. public String getId()
  22. {
  23. return id;
  24. }
  25. public void setId(String id)
  26. {
  27. this.id = id;
  28. }
  29. public String getName()
  30. {
  31. return name;
  32. }
  33. public void setName(String name)
  34. {
  35. this.name = name;
  36. }
  37. public String getDisplayName()
  38. {
  39. return displayName;
  40. }
  41. public void setDisplayName(String displayName)
  42. {
  43. this.displayName = displayName;
  44. }
  45. @Override
  46. public String toString()
  47. {
  48. return "Role{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", displayName='" + displayName + '\'' + '}';
  49. }
  50. }

User.java

  1. package com.shrimpking.pojo;
  2. import org.springframework.security.core.GrantedAuthority;
  3. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  4. import org.springframework.security.core.userdetails.UserDetails;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.List;
  8. /**
  9. * Created by IntelliJ IDEA.
  10. *
  11. * @Author : Shrimpking
  12. * @create 2023/6/6 18:29
  13. */
  14. public class User implements UserDetails
  15. {
  16. private int id;
  17. private String userName;
  18. private String password;
  19. private boolean enabled;
  20. private boolean locked;
  21. private List<Role> roles;
  22. public User()
  23. {
  24. }
  25. public User(String userName, String password)
  26. {
  27. this.userName = userName;
  28. this.password = password;
  29. }
  30. public int getId()
  31. {
  32. return id;
  33. }
  34. public void setId(int id)
  35. {
  36. this.id = id;
  37. }
  38. public String getUserName()
  39. {
  40. return userName;
  41. }
  42. public void setUserName(String userName)
  43. {
  44. this.userName = userName;
  45. }
  46. /**
  47. * 获取当前用户对象的密码
  48. * @return
  49. */
  50. @Override
  51. public String getPassword()
  52. {
  53. return password;
  54. }
  55. public void setPassword(String password)
  56. {
  57. this.password = password;
  58. }
  59. /**
  60. * 当前账号是否可用
  61. * @return
  62. */
  63. @Override
  64. public boolean isEnabled()
  65. {
  66. return enabled;
  67. }
  68. public void setEnabled(boolean enabled)
  69. {
  70. this.enabled = enabled;
  71. }
  72. public boolean isLocked()
  73. {
  74. return locked;
  75. }
  76. public void setLocked(boolean locked)
  77. {
  78. this.locked = locked;
  79. }
  80. public List<Role> getRoles()
  81. {
  82. return roles;
  83. }
  84. public void setRoles(List<Role> roles)
  85. {
  86. this.roles = roles;
  87. }
  88. @Override
  89. public String toString()
  90. {
  91. return "User{" + "id=" + id + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", enabled=" + enabled + ", locked=" + locked + ", roles=" + roles + '}';
  92. }
  93. /**
  94. * 获取当前用户对象所具有的角色信息
  95. * @return
  96. */
  97. @Override
  98. public Collection<? extends GrantedAuthority> getAuthorities()
  99. {
  100. List<SimpleGrantedAuthority> authorities = new ArrayList<>();
  101. for (Role role : roles)
  102. {
  103. authorities.add(new SimpleGrantedAuthority(role.getName()));
  104. }
  105. return authorities;
  106. }
  107. /**
  108. * 获取当前用户对象的用户名
  109. * @return
  110. */
  111. @Override
  112. public String getUsername()
  113. {
  114. return userName;
  115. }
  116. /**
  117. * 当前账号是否未过期
  118. * @return
  119. */
  120. @Override
  121. public boolean isAccountNonExpired()
  122. {
  123. return true;
  124. }
  125. /**
  126. * 当前账号是否未锁定
  127. * @return
  128. */
  129. @Override
  130. public boolean isAccountNonLocked()
  131. {
  132. return !locked;
  133. }
  134. /**
  135. * 当前账号密码是否未过期
  136. * @return
  137. */
  138. @Override
  139. public boolean isCredentialsNonExpired()
  140. {
  141. return true;
  142. }
  143. }

代码解释:

• 用户实体类需要实现UserDetails接口,并实现该接口中的7个方法,getAuthorities,getUsername,isAccountNonExpired,isAccountNonLocked,isCredentialsNonExpired,isEnabled,getPassword。

用户根据实际情况设置这7个方法的返回值。因为默认情况下不需要开发者自己进行密码角色等信息的比对,开发者只需要提供相关信息即可,例如getPassword()方法返回的密码和用户输入的登录密码不匹配,会自动抛出BadCredentialsException异常,isAccountNonExpired()方法返回了false,会自动抛出AccountExpiredException异常,因此对开发者而言,只需要按照数据库中的数据在这里返回相应的配置即可。本案例因为数据库中只有enabled和locked字段,故账户未过期和密码未过期两个方法都返回true。

• getAuthorities()方法用来获取当前用户所具有的角色信息,本案例中,用户所具有的角色存储在roles属性中,因此该方法直接遍历roles属性,然后构造SimpleGrantedAuthority集合并返回。

创建UserService

接下来创建UserService,代码如下:

  1. package com.shrimpking.service;
  2. import com.shrimpking.mapper.UserMapper;
  3. import com.shrimpking.pojo.User;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.security.core.userdetails.UserDetails;
  6. import org.springframework.security.core.userdetails.UserDetailsService;
  7. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  8. import org.springframework.stereotype.Service;
  9. /**
  10. * Created by IntelliJ IDEA.
  11. *
  12. * @Author : Shrimpking
  13. * @create 2023/6/6 19:01
  14. */
  15. @Service
  16. public class UserService implements UserDetailsService
  17. {
  18. @Autowired
  19. private UserMapper userMapper;
  20. @Override
  21. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
  22. {
  23. User user = userMapper.loadUserByUsername(username);
  24. if(user == null)
  25. {
  26. throw new UsernameNotFoundException("账号不存在");
  27. }
  28. user.setRoles(userMapper.getUserRolesByUid(user.getId()));
  29. return user;
  30. }
  31. }

代码解释:

定义UserService实现UserDetailsService接口,并实现该接口中的loadUserByUsername方法,该方法的参数就是用户登录时输入的用户名,通过用户名去数据库中查找用户,如果没有查找到用户,就抛出一个账户不存在的异常,如果查找到了用户,就继续查找该用户所具有的角色信息,并将获取到的user对象返回,再由系统提供的DaoAuthenticationProvider类去比对密码是否正确。

• loadUserByUsername方法将在用户登录时自动调用。

还涉及UserMapper和UserMapper.xml,相关源码如下:

UserMapper.java

  1. package com.shrimpking.mapper;
  2. import com.shrimpking.pojo.Role;
  3. import com.shrimpking.pojo.User;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import java.util.List;
  6. /**
  7. * Created by IntelliJ IDEA.
  8. *
  9. * @Author : Shrimpking
  10. * @create 2023/6/6 19:02
  11. */
  12. @Mapper
  13. public interface UserMapper
  14. {
  15. User loadUserByUsername(String username);
  16. List<Role> getUserRolesByUid(int id);
  17. }

UserMapper.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.shrimpking.mapper.UserMapper">
  5. <select id="loadUserByUsername" resultType="com.shrimpking.pojo.User">
  6. select id,userName,password,enabled,locked
  7. from r_user where userName = #{userName}
  8. </select>
  9. <select id="getUserRolesByUid" resultType="com.shrimpking.pojo.Role">
  10. select
  11. r.id,
  12. r.name,
  13. r.displayName
  14. from r_role r,r_user_role ur
  15. where r.id = ur.roleId
  16. and ur.userId = #{id}
  17. </select>
  18. </mapper>

配置Spring Security

接下来对Spring Security进行配置,代码如下:

  1. package com.shrimpking.config;
  2. import com.shrimpking.service.UserService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  9. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  10. import org.springframework.security.crypto.password.NoOpPasswordEncoder;
  11. import org.springframework.security.crypto.password.PasswordEncoder;
  12. /**
  13. * Created by IntelliJ IDEA.
  14. *
  15. * @Author : Shrimpking
  16. * @create 2023/6/6 19:20
  17. */
  18. @Configuration
  19. public class WebSecurityConfig extends WebSecurityConfigurerAdapter
  20. {
  21. @Autowired
  22. private UserService userService;
  23. @Bean
  24. PasswordEncoder passwordEncoder()
  25. {
  26. //return new BCryptPasswordEncoder(10); //默认10,取值范围4-31之间
  27. //本案例使用NoOpPasswordEncoder,即不对密码进行加密。
  28. return NoOpPasswordEncoder.getInstance();
  29. }
  30. @Override
  31. protected void configure(AuthenticationManagerBuilder auth) throws Exception
  32. {
  33. auth.userDetailsService(userService);
  34. }
  35. @Override
  36. protected void configure(HttpSecurity http) throws Exception
  37. {
  38. http.authorizeRequests()
  39. .antMatchers("/admin/**").hasRole("admin")
  40. .antMatchers("/db/**").hasRole("dba")
  41. .antMatchers("/user/**").hasRole("user")
  42. .anyRequest().authenticated()
  43. .and()
  44. .formLogin()
  45. .loginProcessingUrl("/login").permitAll()
  46. .and()
  47. .csrf().disable();
  48. }
  49. }
  50. /*
  51. 首先配置了三个用户,
  52. root用户具备ADMIN和DBA的角色,
  53. admin用户具备ADMIN和USER的角色,
  54. king用户具备USER的角色。
  55. 调用authorizeRequests()方法开启HttpSecurity的配置,
  56. antMatchers配置分别表示用户访问“/admin/**”模式的URL必须具备ADMIN的角色;
  57. antMatchers用户访问“/user/**”模式的URL必须具备ADMIN或USER的角色;
  58. antMatchers用户访问“/db/**”模式的URL必须具备ADMIN和DBA的角色。
  59. anyRequest用户访问URL都必须认证后访问(登录后访问)。
  60. formLogin表示开启表单登录,即读者一开始看到的登录页面,
  61. loginProcessingUrl同时配置了登录接口为“/login”,即可以直接调用“/login”接口,
  62. 发起一个POST请求进行登录,登录参数中用户名必须命名为username,
  63. 密码必须命名为password,
  64. 配置loginProcessingUrl接口主要是方便Ajax或者移动端调用登录接口。
  65. permitAll,表示和登录相关的接口都不需要认证即可访问。
  66. 表示关闭csrf。
  67. */

创建Controller进行测试了,

 HelloController.java

  1. package com.shrimpking.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * Created by IntelliJ IDEA.
  6. *
  7. * @Author : Shrimpking
  8. * @create 2023/6/6 19:29
  9. */
  10. @RestController
  11. public class HelloController
  12. {
  13. @GetMapping("/admin/hello")
  14. public String admin()
  15. {
  16. return "hello admin";
  17. }
  18. @GetMapping("/user/hello")
  19. public String user()
  20. {
  21. return "hello user";
  22. }
  23. @GetMapping("/db/hello")
  24. public String dba()
  25. {
  26. return "hello dba";
  27. }
  28. @GetMapping("/hello")
  29. public String hello()
  30. {
  31. return "hello";
  32. }
  33. }

记得pom.xml中过滤条件的配置

  1. <resources>
  2. <resource>
  3. <directory>src/main/java</directory>
  4. <includes>
  5. <include>**/*.xml</include>
  6. <include>**/*.properties</include>
  7. </includes>
  8. <filtering>false</filtering>
  9. </resource>
  10. <resource>
  11. <directory>src/main/resources</directory>
  12. <includes>
  13. <include>**/*.xml</include>
  14. <include>**/*.properties</include>
  15. </includes>
  16. <filtering>false</filtering>
  17. </resource>
  18. </resources>

 

权限大小依次为 root > admin > king。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/783735
推荐阅读
相关标签
  

闽ICP备14008679号