如何使用Java SpringBoot安全框架去整合Spring Security
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,如何使用Java SpringBoot安全框架去整合Spring Security,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1.工业
千家信息网最后更新 2025年02月23日如何使用Java SpringBoot安全框架去整合Spring Security
如何使用Java SpringBoot安全框架去整合Spring Security,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
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 springsecurityReview 1.0-SNAPSHOT spring-boot-dependencies org.springframework.boot 2.5.4 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-security org.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用户名和密码自定义
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 { //角色和资源的关系 @Override protected 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(); } @Override protected 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 SpringBoot安全框架去整合Spring Security的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
整合
密码
安全
框架
方法
内容
更多
权限
用户
用户名
角色
账号
问题
开发
束手无策
为此
工业级
全家
原因
基础
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
统计局证书助手数据库操作失败
鄞州手机软件开发管理
湖北虎跳网络技术股份有限公司
数据库监听mysql
无法连接到主数据库不支持驱动
m7s流媒体服务器入门教程
嘉兴电话视频系统服务器
7月14网络安全
数据库密码加密软件
普陀区电商软件开发服务要求
sybase 数据库空间
复制数据库数据
方舟怎么通过ip加入服务器
阳泉市网络安全警察
app棋牌软件开发者犯法吗
东营ios软件开发企业
华为网络安全主流产品
软件开发公司费用支出
坦克世界亚服服务器连接已经断开
数据库英语读法
计算机二级数据库软件
游戏ui开发软件开发
长春市阿里巴巴总部网络技术
金华学软件开发工具
中转服务器如何配置
黄浦区咨询软件开发常见问题
松江区网络技术服务生产过程
部队小品网络安全
网络技术大赛 广西
穿越火线为什么总是服务器繁忙