本文共 2285 字,大约阅读时间需要 7 分钟。
1、Spring Security是一个安全组件,为java企业级开发提供了全面的安全防护。它可以在Controller层、Service层、DAO层等以加注解的方式来保护应用程序的安全。SpringSecurity提供了细粒度的权限控制,可以精细到每一个API接口,每一个业务方法,或者每一个DAO层的方法。SpringSecurity提供的是应用程序层的安全解决方案。
2、SpringSecurity对环境无依赖性、低耦合性,提供了数十个安全模块,模块间耦合性低,可以自由组合来定制安全功能。 安全领域的两大模块是认证和授权
; 认证
是要确认身份,可以是执行操作的用户、设备或者其他系统。 授权
是赋予权限,给认证后的身份确定能做什么操作。 3、SpringSecurity采用了注解的方式控制权限,熟悉spring的开发者很容易上手;且很容易集成到springboot工程中。
4、与shiro
对比 shiro一般使用在单体应用中,但在微服务架构中,它是无能为力的。 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
@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功能。
在方法级别上的安全验证是通过相关的注解和配置来实现的。@EnableGlobalMethodSecurity
注解开启了方法级别的保护,参数最常用的是@prePostEnabled
UserDetails
接口来实现SpringSecurity认证信息;Service层通过实现UserDetailsService
接口,根据用户名获取该用户的所有信息,包括yoghurt信息和权限点。 转载地址:http://ibaxb.baihongyu.com/