springsecurity问题

WebSecurityConfigurationAdapter


网络配置的核心类,至关于xml中的注解html

<sec:http></sec:http>

当使用WebSecurityConfigurationAdapter的时候,会从父类继承一个默认的AuthenticationManager.web

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MySecurityConfig extends WebSecurityConfigurerAdapter {


    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

程序有的时候会报`No bean named 'org.springframework.security.authenticationManager' is defined
使用上面的方法有的时候能够解决。spring

在WebSecurityConfigurerAdapter中会有经常使用的配置:json

  • configure(WebSecurity) 经过重载,配置Spring Security的Filter链
  • configure(HttpSecurity) 经过重载,配置如何对http的请求进行拦截
  • configure(AuthenticationManagerBuilder) 经过重载,配置user-detail服务

在 configure(HttpSecurity)中进行配置的时候,必定要把要求比较宽松的放在前面,要求少的放在后面。网络

http.authorizeRequests().antMatchers("/login").permitAll();
 http.authorizeRequests().antMatchers("/", "/home").access("hasRole('USER')")
   .antMatchers("/admin/**").access("hasRole('ADMIN')")
  .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
   .and().formLogin().loginPage("/login").loginProcessingUrl("/login").successHandler(customSuccessHandler)
   .and().exceptionHandling().accessDeniedPage("/Access_Denied");

若是不是这种顺序,有可能形成要求宽松的没法生效。

上面的配置还有一个隐藏的功能就是把默认的login的form表格换成了本身写的login.jsp或者是login.html。具体看web中使用的框架。框架

若是在这里进行了配置,就不要再xml中使用sec:http/来进行配置了。由于若是在xml中进行了配置,会一直提示没有AuthenticationManager。程序觉得是xml中进行配置了,但并无找到,而忽略了在WebSecurityConfigurationAdapter中父类默认的AuthenticationManager。

form的配置


  • loginpage: 表明的程序默认的登录页面
  • loginProcessingUrl: action的名称,例如叫loginform
  • successhandler: 若是是直接继承自AuthenticationSuccessHandler,那必须重写onAuthenticationSuccess,无论后台有没有处理loginform,都会调用这个方法,发送给客户端一个json字符串,客户端根据json字符串在进行加载,这种通常适用权限比较细的系统。 若是继承自SimpleUrlAuthenticationSuccessHandler,那么对loginform没有处理的话就不会进行跳转,这个方法也不会生效。能够设置一个defaulturl。

NameSpace:sec:http</sec:http>等。ssh

当咱们在使用NameSpace时,Spring Security是会自动为咱们创建对应的FilterChain以及其中的Filter。但有时咱们可能须要添加咱们本身的Filter到FilterChain,又或者是由于某些特性须要本身显示的定义Spring Security已经为咱们提供好的Filter,而后再把它们添加到FilterChain。使用NameSpace时添加Filter到FilterChain是经过http元素下的custom-filter元素来定义的。定义custom-filter时须要咱们经过ref属性指定其对应关联的是哪一个Filter,此外还须要经过position、before或者after指定该Filter放置的位置。诚如在上一节《Filter顺序》中所提到的那样,Spring Security对FilterChain中Filter顺序是有严格的规定的。Spring Security对那些内置的Filter都指定了一个别名,同时指定了它们的位置。咱们在定义custom-filter的position、before和after时使用的值就是对应着这些别名所处的位置。如position=”CAS_FILTER”就表示将定义的Filter放在CAS_FILTER对应的那个位置,before=”CAS_FILTER”就表示将定义的Filter放在CAS_FILTER以前,after=”CAS_FILTER”就表示将定义的Filter放在CAS_FILTER以后。此外还有两个特殊的位置能够指定,FIRST和LAST,分别对应第一个和最后一个Filter,如你想把定义好的Filter放在最后,则可使用after=”LAST”。jsp

相关文章
相关标签/搜索