我有使用java配置方法配置Spring安全性的Spring Web应用程序.我想从身份验证中排除一些URL模式(例如:静态资源等).我之前使用spring security xml config做了这个,但是无法弄清楚java配置,因为添加antmatchers没有帮助.
以下是我在扩展WebSecurityConfigurerAdapter的安全配置类中添加的代码
@Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/authFailure") .permitAll() .anyRequest() .authenticated() .and() .httpBasic() .and() .authenticationProvider(_provider) .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .addFilter(authFilter()) .addFilterAfter(executionContextFilter(), TokenBasedSecurityFilter.class).csrf().disable(); }
我使用的弹簧安全版本是3.2.0.在此先感谢您的帮助
编辑:
我在点击排除的网址时得到的堆栈跟踪,
org.springframework.security.authentication.AuthenticationServiceException: Authorization Header is not available in the request at com.inventory.ricemill.tba.spring.TokenBasedSecurityFilter.doFilter(TokenBasedSecurityFilter.java:59) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744) Apr 01, 2014 10:18:41 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [Inventory] in context with path [/ricemill] threw exception [Request processing failed; nested exception is org.springframework.security.authentication.AuthenticationServiceException: Authorization Header is not available in the request] with root cause org.springframework.security.authentication.AuthenticationServiceException: Authorization Header is not available in the request at com.inventory.ricemill.tba.spring.TokenBasedSecurityFilter.doFilter(TokenBasedSecurityFilter.java:59) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744)
请求通过spring安全过滤器链中注册的过滤器,而不应该使用antmatcher忽略请求
在Github上发布了Spring安全示例中的解决方案.
WebSecurityConfigurerAdapter
有一个重载configure
消息,WebSecurity
它接受作为参数,接受要被忽略的请求的蚂蚁匹配器.
@Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/authFailure"); }
有关详细信息,请参阅Spring Security Samples
您在哪里配置经过身份验证的网址格式?我只在你的代码中看到一个uri.
你有多个配置(HttpSecurity)方法或只有一个?看起来您需要在一个方法中使用所有URI.
我有一个网站,需要身份验证才能访问所有内容,所以我想保护/*.但是为了进行身份验证,我显然不想保护/登录.我也有静态资产,我想允许访问(所以我可以使登录页面很漂亮)和一个不需要auth的健康检查页面.
另外我有一个资源/ admin,它需要比网站其他部分更高的权限.
以下是为我工作.
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login**").permitAll() .antMatchers("/healthcheck**").permitAll() .antMatchers("/static/**").permitAll() .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") .antMatchers("/**").access("hasRole('ROLE_USER')") .and() .formLogin().loginPage("/login").failureUrl("/login?error") .usernameParameter("username").passwordParameter("password") .and() .logout().logoutSuccessUrl("/login?logout") .and() .exceptionHandling().accessDeniedPage("/403") .and() .csrf(); }
注意:这是第一场比赛胜利,因此您可能需要玩订单.例如,我最初有/**:
.antMatchers("/**").access("hasRole('ROLE_USER')") .antMatchers("/login**").permitAll() .antMatchers("/healthcheck**").permitAll()
这导致网站不断将/ login的所有请求重定向回/ login.同样我最后/ admin/**:
.antMatchers("/**").access("hasRole('ROLE_USER')") .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
这导致我的未经验证的测试用户"访客"可以访问管理界面(yikes!)
当你说添加antMatchers没有帮助 - 你的意思是什么?antMatchers正是你如何做到的.像下面这样的东西应该工作(显然适当地改变你的URL):
@Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/authFailure").permitAll() .antMatchers("/resources/**").permitAll() .anyRequest().authenticated()
如果你还没有任何快乐,那么你需要提供更多细节/堆栈跟踪等.
XML to Java config switch的详细信息在这里