Spring Security (1)

Spring Security 模块划分

  1. ACL 支持通过访问控制列表(access control list,ACL)为域对象提供安全性
  2. 切面(Aspects) 一个很小的模块,当使用Spring Security注解时,会使用基于AspectJ的切面,而不是使用标准的Spring AOP
  3. CAS客户端(CAS Client) 提供Jasig的中心认证服务(Central Authentication Service, CAS)进行集成的功能
  4. 配置(Configuration) 包含通过XML和Java配置Spring Security的功能支持
  5. 核心(core) 提供Spring Security基本库
  6. 加密(Cryptography) 提供加密和密码编码的功能
  7. LDAP 支持基于LDAP进行认证
  8. OpenID 支持使用OpenID进行集中式认证
  9. Remoting 提供了对Spring Remoting的支持
  10. 标签库(Tag Library) Spring Security的JSP标签库
  11. Web 提供了Spring Security基于Filter的Web安全性支持

Application 的classpath里面至少要包含Core和Configuration这两个模块

简单配置Spring Security

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{}

任何实现WebSecurityConfigure的类都可以用来配置Spring Security , 但是通常我们使用更简单的方式:扩展WebSecurityConfigure的实现类WebSecurityConfigurerAdapter。
具体的配置可以通过重写WebSecurityConfigurerAdapter的configure()方法来实现,下面是三个方法的功能:

  1. configure(HttpSecurity) //通过重载,配置如何通过拦截器保护请求
  2. configure(WebSecurity) //通过重载,配置Spring Security的Filter链
  3. configure(AuthenticationManagerBuilder) //通过重载,配置user-detail服务

Demo Code:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/assets/**","/bower_components/**","/config/**","/controllers/**","/service/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error")
                .permitAll()
                .and()
                .rememberMe()
                .and()
                .logout()
                .permitAll()
                .and()
                .csrf()
                .disable();
    }

SPEL

Spring Security通过一些安全性相关的表达式扩展了Spring表达式语言

  1. authentication // 用户的认证对象
  2. denyAll // 结果始终未false
  3. hasAnyRole(list of roles) // 如果用户被授予了列表中任意的指定角色,结果为true
  4. hasRole(role) // 如果用户被授予了指定的角色,结果为true
  5. hasIPAddress(IP Address) // 如果请求来自指定IP的话,结果为true
  6. isAnonymous() // 如果当前用户为匿名用户,结果为true
  7. isAuthenticated() // 如果当前用户进行了认证的话,结果为true
  8. isFullyAuthenticated() // 如果当前用户进行了完整认证的话(不是通过Remember-me功能进行的认证),结果为true
  9. isRememberMe() // 如果当前用户是通过Remember-me自动认证的,结果为true
  10. permitAll // 结果始终为true
  11. principal // 用户的principal对象
    spELl的特点就是可以对各种控制进行自由的搭配,比如下面这个
http.authorizeRequests()
                .regexMatchers("/*")
                .access("hasRole('ROLE_ADMIN') and hasIpAddress('192.168.1.2')");
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容