博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安全模块springboot security
阅读量:2376 次
发布时间:2019-05-10

本文共 2285 字,大约阅读时间需要 7 分钟。

为什么用springboot security?

1、Spring Security是一个安全组件,为java企业级开发提供了全面的安全防护。它可以在Controller层、Service层、DAO层等以加注解的方式来保护应用程序的安全。SpringSecurity提供了细粒度的权限控制,可以精细到每一个API接口,每一个业务方法,或者每一个DAO层的方法。SpringSecurity提供的是应用程序层的安全解决方案。

2、SpringSecurity对环境无依赖性、低耦合性,提供了数十个安全模块,模块间耦合性低,可以自由组合来定制安全功能。
安全领域的两大模块是认证和授权
认证是要确认身份,可以是执行操作的用户、设备或者其他系统。
授权是赋予权限,给认证后的身份确定能做什么操作。

3、SpringSecurity采用了注解的方式控制权限,熟悉spring的开发者很容易上手;且很容易集成到springboot工程中。

4、与shiro对比
shiro一般使用在单体应用中,但在微服务架构中,它是无能为力的。

springboot security和spring security的关系

spring security框架中,主要包含两个jar:

org.springframework.security
spring-security-web
org.springframework.security
spring-security-web

springboot对spring security框架做了封装,并没有改动这两个包的内容

org.springframework.boot
spring-boot-starter-security

配置Spring Security

@EnableWebSecurity@Configuration@EnableGlobalMethodSecurity(prePostEnabled = true)public class SecurityConfig extends WebSecurityConfigurerAdapter {   @Override   protected void configure(HttpSecurity http) throws Exception {      http            .authorizeRequests()               .antMatchers("/css/**", "/index").permitAll()               .antMatchers("/user/**").hasRole("USER")                .antMatchers("/qirui/**").hasRole("USER")               .and()            .formLogin().loginPage("/login").failureUrl("/login-error")            .and()            .exceptionHandling().accessDeniedPage("/401");      http.logout().logoutSuccessUrl("/");   }   @Autowired   UserDetailsService userDetailsService;   @Autowired   public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {      auth.userDetailsService(userDetailsService);   }

以上配置类添加@EnableWebSecurity注解,开启WebSecurity功能。

  • 每个请求都要认证
  • 自动生成一个登陆表单
  • 可以用username和password来进行认证
  • 用户可以注销
  • 阻止了CSRF攻击
  • 排除掉了/css和/index开头的资源验证
  • 以/user开头的资源需要验证,并且需要的角色是Role
  • 表单的登录地址是/login,登录失败的地址为/login-error
  • 异常处理会重定向到/401
  • 注销成功,重定向到首页
  • @EnableGlobalMethodSecurity(prePostEnabled = true)开启方法级别的保护
    同时需要配置Controller来完成web跳转。

Spring Security方法级别的保护

在方法级别上的安全验证是通过相关的注解和配置来实现的。@EnableGlobalMethodSecurity注解开启了方法级别的保护,参数最常用的是@prePostEnabled

生产上一般从数据库中读取用户和用户的角色权限信息,springcloud中常用UserDetails接口来实现SpringSecurity认证信息;Service层通过实现UserDetailsService接口,根据用户名获取该用户的所有信息,包括yoghurt信息和权限点。

转载地址:http://ibaxb.baihongyu.com/

你可能感兴趣的文章
HDFS快照与配额
查看>>
优秀视频教程分享
查看>>
hive 数据类型
查看>>
Hbase master HA使用
查看>>
Hbase region 管理
查看>>
Hbase 计数器
查看>>
排序算法 -- 直接插入排序
查看>>
树的基本概念
查看>>
Java命令行工具 -- jps
查看>>
storm 单机环境搭建
查看>>
MongoDB 副本集环境搭建
查看>>
sqoop导出avro格式文件报错
查看>>
spark sql 访问Hive数据表
查看>>
Ambari 启动 oozie UI
查看>>
mongodb报错:com.mongodb.MongoCursorNotFoundException
查看>>
mongodb报错:com.mongodb.MongoSocketReadException: Exception receiving message
查看>>
spark本地程序报错
查看>>
spark从postgresql导入数据至mongodb报错: Decimal precision 39 exceeds max precision 38
查看>>
BufferedReader读取中文文本乱码
查看>>
内网环境centos7 yum离线安装软件包
查看>>