Spring Security Configurer - Go语言中文社区

Spring Security Configurer


刚刚开始接触Spring Security,查了网上一些资料在这里总结一下。

1.Spring Security Configurer

Spring Security Config模块一共有3个builder,认证相关的AuthenticationManagerBuilder和web相关的WebSecurity、HttpSecurity。

2.AuthenticationManagerBuilder

AuthenticationManagerBuilder用来配置全局的认证相关的信息,其实就是AuthenticationProvider和UserDetailsService,前者是认证服务提供商,后者是用户详情查询服务。

认证是通过AuthenticationManager来管的,

public interface AuthenticationManager {

  public Authentication authenticate(Authentication authentication)
      throws AuthenticationException;

}

具体的工作是交给各个 authentication provider来做的:

image

这里provider manager包含多个具体的providers:

<bean id="authenticationManager" 
    class="org.acegisecurity.providers.ProviderManager">
  <property name="providers">
    <list>
      <ref bean="daoAuthenticationProvider"/>
      <ref bean="ldapAuthenticationProvider"/>
    </list>
  </property>
</bean>
ProviderManager is given its list of authentication providers through its providers property.

以DaoAuthenticationProvider举例:

<bean id="authenticationProvider" 
    class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
  <property name="userDetailsService"
      ref="userDetailsService"/>
</bean>

它会要求一个UserDetailsService, 跟它相关的是UserDetails接口

UserDetailsService接口是个简单的接口

public interface UserDetailsService {
    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException;
}

 

UserDetails接口如下:

public interface UserDetails extends Serializable {
    GrantedAuthority[] getAuthorities();

    String getPassword();

    String getUsername();

    boolean isAccountNonExpired();

    boolean isAccountNonLocked();

    boolean isCredentialsNonExpired();

    boolean isEnabled();
}

解释一下getAuthorities:该方法返回一个GrantedAuthority[]数组对象,GrantedAuthority是用户权限信息对象,这个对象中定义了一个获取用户权限描述信息的getAuthority()方法。

需要注意Authentication对象才是Spring Security使用的进行安全访问控制用户信息安全对象。实际上,Authentication对象有未认证和已认证两种状态,在作为参数传入认证管理器(AuthenticationManager)的authenticate方法时,是一个未认证的对象,它从客户端获取用户的身份信息(如用户名,密码),可以是从一个登录页面,也可以从Cookie中获取,并由系统自动构造成一个Authentication对象。而这里提到的UserDetails代表一个用户安全信息的源(从数据库,LDAP服务器,CA中心返回),Spring Security要做的就是将这个未认证的Authentication对象和UserDetails进行匹配,成功后将UserDetails中的用户权限信息拷贝到Authentication中组成一个完整的Authentication对象,共其它组件共享。


3.WebSecurity

LogoutConfigurer
RequestMatcherConfigurer:spring mvc style、ant style、regex style
HeadersConfigurer:
CorsConfigurer、CsrfConfigurer
SessionManagementConfigurer:
PortMapperConfigurer:
JeeConfigurer:
X509Configurer:
RememberMeConfigurer:
ExpressionUrlAuthorizationConfigurer:
RequestCacheConfigurer:
ExceptionHandlingConfigurer:
SecurityContextConfigurer:
ServletApiConfigurer:
ChannelSecurityConfigurer:
此模块的authenticationProvider和userDetailsService;

SecurityFilterChain控制

4.WebSecurityConfigurerAdapter

spring security为web应用提供了一个WebSecurityConfigurerAdapter适配器,应用里spring security相关的配置可以通过继承这个类来编写;具体是提供了上边三个顶级配置项构建器的构建重载回调方法;

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}
public void configure(WebSecurity web) throws Exception {
}
protected void configure(HttpSecurity httpSecurity) throws Exception {
}

5.具体配置思路

  1. httpSecurity.authorizeRequests()返回一个ExpressionInterceptUrlRegistry对象,这个对象就一个作用,注册intercept url规则权限匹配信息,通过设置URL Matcher,antMatchers,mvcMatchers,regexMatchers或者直接设置一个一个或者多个RequestMatcher对象;

  2. 上边设置matchers的方法会返回一个AuthorizedUrl对象,用于接着设置符合其规则的URL的权限信息,AuthorizedUrl对象提供了access方法用于设置一个权限表达式,比如说字符串“hasRole(‘ADMIN’) and hasRole(‘DBA’)”,同时提供了多个方便的语义方法,比如说:

public ExpressionInterceptUrlRegistry hasRole(String role) 
public ExpressionInterceptUrlRegistry hasAuthority(String authority)

这些方法返回值是ExpressionInterceptUrlRegistry,用于接着设置下一条过滤规则:

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/resources/**", "/signup", "/about").permitAll()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
            .anyRequest().authenticated()
            .and()
        // ...
        .formLogin();
}

上边1和2结合起来的功能相当于标签的功能;
UrlAuthorizationConfigurer能实现上边类似的功能;

protected void configure(HttpSecurity http) throws Exception {
    http.apply(new UrlAuthorizationConfigurer<HttpSecurity>()).getRegistry()
            .antMatchers("/users**", "/sessions/**").hasRole("USER")
            .antMatchers("/signup").hasRole("ANONYMOUS").anyRequest().hasRole("USER");
 }
  1. formLogin和logout
    • FormLoginConfigurer
    • OpenIDLoginConfigurer
    • HttpBasicConfigurer
    • LogoutConfigurer

参考:

https://blog.csdn.net/chen517611641/article/details/73277337

http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html(很详细)




版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ljj20161221/article/details/80179960
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:49:16
  • 阅读 ( 1139 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢