Spring 是非常流行和成功的 Java 应用开发框架。Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。
核心功能:
- 用户认证(Authentication):系统判断用户是否能登录
- 用户授权(Authorization):系统判断用户是否有权限去做某些事情
项目结构 代码部分特点:
- Spring 技术栈的组成部分,与Spring 无缝整合。
- 全面的权限控制,能提供完整可扩展的认证和授权支持保护
- 专门为 Web开发而设计。
- 重量级,需要引入各种家族组件与依赖
1、pom文件
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.2.2
com.baomidou
mybatis-plus-boot-starter
3.4.2
org.springframework.boot
spring-boot-starter-security
org.thymeleaf.extras
thymeleaf-extras-springsecurity5
org.projectlombok
lombok
2、yml配置文件
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/websocket
driver-class-name: com.mysql.jdbc.Driver
thymeleaf:
cache: false
#prefix: classpath:/templates/
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
typeAliasesPackage: com.iss.security.*.entity
# mapperLocations: classpath:mapper
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.exceptionHandling()
.accessDeniedPage("/403.html");
http.formLogin()
//登陆页面设置
.loginPage("/index.html")
//登陆访问路径
.loginProcessingUrl("/user/login")
//登陆成功后跳转路径
.defaultSuccessUrl("/success.html").permitAll()
.and()
.authorizeRequests()
//设置不需要认证的访问路径,可以直接访问
.antMatchers("/", "/user/login").permitAll()
//当前用户只有具有addUser权限时才能访问该路径
// .antMatchers("/test/addUser").hasAuthority("addUser")
// .antMatchers("/test/findAll").hasAnyAuthority("addUser,findAll")
// .antMatchers("/test/hello").hasRole("admin")
// .antMatchers("/test/hello").hasAnyRole("admin")
.anyRequest().authenticated()
.and()
//关闭csrf的保护
.csrf().disable();
http.logout()
//退出的地址
.logoutUrl("/logout")
.logoutSuccessUrl("/index.html").permitAll();
}
@Bean
PasswordEncoder password() {
return new BCryptPasswordEncoder();
}
}
4、MyUserDetailsService
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iss.security.login.entity.SysUser;
import com.iss.security.login.mapper.SysUserMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {
@Resource
private SysUserMapper sysUserMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//调用usersMapper方法,根据用户名查询数据库
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("user_name",username);
SysUser users = sysUserMapper.selectOne(wrapper);
if(users==null){
//数据库没有数据,认证失败
throw new UsernameNotFoundException("用户名不存在!");
}
//手动设置权限,也可以通过数据库查询获取
List auths = AuthorityUtils
.commaSeparatedStringToAuthorityList("addUser,findAll,ROLE_admin,ROLE_user");
return new User(users.getUserName(),
new BCryptPasswordEncoder().encode(users.getPassword()),auths);
}
}
5、entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SysUser implements Serializable {
private Integer id;
private String userName;
private String password;
private String qqMail;
private String role;
}
6、mybatis-plus
7、html
Title
权限控制:
SpringBoot整合SpringSecurity超详细入门教程



