SpringSecurity入门(一)

超详细的Java知识点汇总


Spring Security简介

SpringSecurity是一个强大且高效的安全框架,能够提供用户验证和访问控制服务,能够很好地整合到以Spring为基础的项目中。
SpringBoot对SpringSecurity进行了大量的自动配置,使开发者通过少量的代码和配置就能完成很强大的验证和授权功能,下面我们就体验下SpringSecurity的基本使用。

入门案例

引入spring security依赖后就会出现自带的登录效果:

  1. 相关依赖
    这里使用SpringBoot版本是2.4.4,SpringSecurity版本是5.4.5
 <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.4.4</version>
     <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <dependencies>
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-security</artifactId>
	</dependency>
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>
  1. 测试页面
    在templates目录中添加页面:main.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Main</title>
</head>
<body>
    <h1>Welcome to Main</h1>
</body>
</html>
  1. 控制器
@Controller
public class UserController {

    @RequestMapping("/main")
    public String main(){
        return "main";
    }
 }
  1. 启动项目后,控制台会打印密码
    在这里插入图片描述
    访问页面 http://localhost:8080/main 时,会出现自带的登录页面
    用户名默认为user,密码就是前面打印出来的
    在这里插入图片描述
    登录成功后,看到main页面
    在这里插入图片描述

自定义登录

项目中的登录功能肯定还是要自己开发,如何开发自己的登录功能呢?

  1. 定义登录页面login.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--出现了验证错误后,实现下面内容-->
    <div th:if="${param.error}">
        <p style="text-align: center" class="text-danger">登录失败,账号或密码错误!</p>
    </div>
    <!--这里是提交给SpringSecurity配置的登录URL处理-->
    <form th:action="@{/login}" method="post">
    	<!--这里注意:名称必须是username和password,SpringSecurity默认指定的-->
        <input type="text" name="username" placeholder="Input your username"><br>
        <input type="password" name="password" placeholder="Input your password"><br>
        <input type="submit" value="Login">
    </form>
</body>
</html>
  1. Controller添加方法
@RequestMapping("/login")
public String login(){
    return "login";
}
  1. Web验证的配置
/**
 * 启动Web安全验证
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 返回密码编码器
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     * 配置用户账号密码以及角色
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //在内存中创建用户
        auth.inMemoryAuthentication()
                //账号
                .withUser("admin")
                //密码,需要加密
                .password(new BCryptPasswordEncoder().encode("123"))
                //添加角色
                .roles("ADMIN","USER")
                //创建另一个用户
                .and()
                .withUser("user")
                .password(new BCryptPasswordEncoder().encode("123"))
                .roles("USER");
    }

    /**
     * 配置web页面的权限
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //用户请求授权
        http.authorizeRequests()
                //指定登录相关的请求,permitAll是不需要验证
                .antMatchers("/login").permitAll()
                //指定/user/** 需要USER角色
                .antMatchers("/user/**").hasRole("USER")
                .antMatchers("/admin/**").hasRole("ADMIN")
                //其它所有URL都需要验证
                .anyRequest().authenticated()
                .and()
                //配置登录URL为login,登录成功后跳转main
                .formLogin().loginPage("/login").defaultSuccessUrl("/main")
                .and()
                //配置注销url,注销后到登录页面
                .logout().logoutUrl("/logout").logoutSuccessUrl("/login");
    }
}
  1. 测试login,输入上面配置的账号和密码
    在这里插入图片描述

登录成功
在这里插入图片描述
账号密码填写错误
在这里插入图片描述

密码处理

SpringSecurity登录验证使用的密码必须要经过加密处理,这里提供了PasswordEncoder接口进行密码加密。
PasswordEncoder接口提供两个主要方法:

  • String encode(CharSequence rawPassword)
    将原始密码加密,返回密文
  • boolean matches(CharSequence rawPassword,String password)
    将第一个参数原始密码和第二个参数密文进行匹配,返回是否匹配成功

PasswordEncoder的常用实现类是:BCryptPasswordEncoder
BCryptPasswordEncoder是基于hash算法的单向加密,可以控制密码强度,默认为10。
在上面的配置类中,配置了该加密器

@Bean
 public PasswordEncoder passwordEncoder(){
     return new BCryptPasswordEncoder();
 }

做下测试

@SpringBootTest
class SpringSecurityDbDemoApplicationTests {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Test
    void contextLoads() {
        for (int i = 0; i < 5; i++) {
            String encode = passwordEncoder.encode("123456");
            System.out.println("encode:"+encode);
            System.out.println("matches:"+passwordEncoder.matches("123456",encode));
        }}

}

在这里插入图片描述
可以看到同样是对"123456"进行加密,每次得到的密文都不相同,但是每次都可以匹配成功。
不同于另一个常用的安全框架:Shiro,SpringSecurity不需要给密码单独配置盐,盐是随机生成的,这样密码的安全性更高。

授权控制

在创建用户时,除了账号密码外,还可以添加对应的角色和权限,如:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
         //在内存中创建用户
        auth.inMemoryAuthentication()
                //账号
                .withUser("admin")
                //密码,需要加密
                .password(new BCryptPasswordEncoder().encode("123"))
                //添加角色
                .roles("ADMIN","USER")
                //创建另一个用户
                .and()
                .withUser("user")
                .password(new BCryptPasswordEncoder().encode("123"))
                //也可以通过authorities添加权限和角色,如果是角色需要以ROLE_开头
                .authorities("LIST","ROLE_USER");
}

给指定的URL配置角色和权限,这样就可以进行访问控制了

@Override
protected void configure(HttpSecurity http) throws Exception {
    //用户请求授权
    http.authorizeRequests()
            //指定toLogin请求,permitAll不需要验证
            .antMatchers("/login").permitAll()
            //指定/user/** 需要USER角色
            .antMatchers("/user/**").hasRole("USER")
            .antMatchers("/admin/**").hasRole("ADMIN")
            //需要LIST权限
            .antMatchers("/admin/**").hasAuthority("LIST")
            //其它所有URL都需要验证
            .anyRequest().authenticated()
            .and()
            //配置登录页面为login,登录成功后跳转main
            .formLogin().loginPage("/login").defaultSuccessUrl("/main")
            .and()
            //配置注销url,注销后到登录页面
            .logout().logoutUrl("/logout").logoutSuccessUrl("/login");
}

修改控制器的/main方法

@RequestMapping("/main")
public String main(Model model){
    //读取验证对象
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    //读取用户
    Object principal = authentication.getPrincipal();
    //如果是登录用户,则为org.springframework.security.core.userdetails.User
    if(principal instanceof User){
        User user = (User) principal;
        //读取用户名
        model.addAttribute("username",user.getUsername());
        //读取所有权限
        model.addAttribute("authorities",user.getAuthorities());
    }else {
        model.addAttribute("username", principal);
    }
    return "main";
}

修改main.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Main</title>
</head>
<body>
    <h1 th:text="|Welcome!${username}|">Welcome!</h1>
    你的权限有:
    <span th:each="auth:${authorities}">
        [[${auth}]]
    </span>
    <p><a href="/admin/admin">管理员页面</a> </p>
    <p><a href="/user/user">用户页面</a> </p>
    <p><a href="/login">登录页面</a> </p>

    <p>
        <form th:action="@{/logout}" method="post">
        <input type="submit" value="注销">
        </form>
    </p>
</body>
</html>

用admin登录,看到的权限是两个角色:ROLE_ADMIN和ROLE_USER,ROLE_是自动添加到角色上的。
在这里插入图片描述
用user登录
在这里插入图片描述
再试一下访问不同的URL
添加目录和文件:admin/admin.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Admin</title>
</head>
<body>
    <h1 th:text="|Hello!${username},欢迎进入管理员页面|">Admin</h1>
</body>
</html>

user/user.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>User</title>
</head>
<body>
    <h1 th:text="|Hello!${username},欢迎进入用户页面|">User</h1>
</body>
</html>

错误页面:error/403.html,这里/error是Security默认的错误地址,添加/error/错误代码.html 后,出现对应错误时会自动跳转到对应页面,403是权限不足。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>403</title>
</head>
<body>
    Sorry!!你的权限不足
</body>
</html>

控制器添加方法:

@RequestMapping("/user/user")
 public String user(Model model){
     Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
     Object principal = authentication.getPrincipal();
     if(principal instanceof User){
         User user = (User) principal;
         model.addAttribute("username",user.getUsername());
         model.addAttribute("authorities",user.getAuthorities());
     }else {
         model.addAttribute("username", principal);
     }
     return "user/user";
 }

 @RequestMapping("/admin/admin")
 public String admin( Model model){
     Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
     Object principal = authentication.getPrincipal();
     if(principal instanceof User){
         User user = (User) principal;
         model.addAttribute("username",user.getUsername());
         model.addAttribute("authorities",user.getAuthorities());
     }else {
         model.addAttribute("username", principal);
     }
     return "admin/admin";
 }

用admin登录后,访问管理员的超链接,出现了权限不足
在这里插入图片描述
访问用户超链接正常
在这里插入图片描述
admin登录后只有ADMIN和USER角色,没有LIST权限,所以不能访问/admin/admin,可以修改创建admin用户时的授权配置,就可以访问了

//添加角色
// .roles("ADMIN","USER")
//添加ADMIN、USER角色和LIST权限,如果是角色需要以ROLE_开头
.authorities("LIST","ROLE_ADMIN","ROLE_USER")

总结

以上我们完成了SpringSecurity的基本登录验证功能,以及密码和授权的配置,真正开发时肯定是需要进行数据库验证的,下一章再说。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恒哥~Bingo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值