Java|Java SpringBoot安全框架整合Spring Security详解
目录
- 1.工业级安全框架介绍
- 2.建议搭建Spring Security环境
- 2.1在pom.xml中添加相关依赖
- 2.2创建Handler类
- 2.3创建简单的html和配置相关thymeleaf的路径
- 2.4最后再加个启动类,那么我们的整合测试就完成勒
- 2.5成果展示 用户名默认user,密码则随机生成的这串数字
- 3.进阶版使用
- 3.1用户名和密码自定义
- 3.2在config包下创建Encoder
- 3.3赋予账号角色权限
- 总结
1.工业级安全框架介绍 Spring Security基于Spring开发,项目中如果使用Spring作为基础,配合Spring Security做权限更加方便,而Shiro需要和Spring进行整合开发。因此作为spring全家桶中的Spring Security在java领域很常用。
2.建议搭建Spring Security环境
2.1在pom.xml中添加相关依赖
4.0.0 org.example springsecurityReview1.0-SNAPSHOT spring-boot-dependenciesorg.springframework.boot 2.5.4 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-securityorg.springframework.boot spring-boot-starter-thymeleaf
2.2创建Handler类
package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controllerpublic class Handler {@GetMapping("/index")public String index(){return "index"; }}
2.3创建简单的html和配置相关thymeleaf的路径
文章图片
2.4最后再加个启动类,那么我们的整合测试就完成勒
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args); }}
2.5成果展示 用户名默认user,密码则随机生成的这串数字
文章图片
文章图片
3.进阶版使用
3.1用户名和密码自定义
【Java|Java SpringBoot安全框架整合Spring Security详解】
文章图片
3.2在config包下创建Encoder
进行密码的校验和转码操作,将密码转成字符串形式,并通过match方法惊醒校验。
文章图片
3.3赋予账号角色权限
package com.example.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {//角色和资源的关系@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN").antMatchers("/index").access("hasRole('ADMIN') or hasRole('USER') ").anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout().permitAll().and().csrf().disable(); }@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("user").password(new MyPasswordEncoder().encode("000")).roles("USER").and().withUser("admin").password(new MyPasswordEncoder().encode("123")).roles("ADMIN","USER"); }}
最后达到admin账号能访问admin.html和index.html
user只能访问index.html的操作
总结 本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- Activiti(一)SpringBoot2集成Activiti6
- 事件代理
- SpringBoot调用公共模块的自定义注解失效的解决
- Java|Java OpenCV图像处理之SIFT角点检测详解
- 解决SpringBoot引用别的模块无法注入的问题
- java中如何实现重建二叉树
- 数组常用方法一
- NeuVector 会是下一个爆款云原生安全神器吗()
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题