2015-10-07 108 views
0

我遇到了Spring Security 4.0的麻烦。春季安全表单登录REST应用程序

我使用Spring Security来保护Web应用程序和一些REST资源,这些资源将通过使用Ajax调用被移动应用程序使用。 我的web应用程序开箱即用,但我的REST服务不太好。这个想法是登录到我的应用程序使用表单登录在我的http,但成功登录后,我无法访问我的受保护的资源

要进入代码,这里是我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0"> 
<display-name>ShakePoint CMS</display-name> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/datasource.xml 
     /WEB-INF/repositories.xml 
     /WEB-INF/facades.xml 
     /WEB-INF/spring-mvc-servlet.xml 
     /WEB-INF/spring-security.xml 
     </param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<servlet> 
    <servlet-name>spring-mvc</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class> 
     org.springframework.web.filter.DelegatingFilterProxy 
    </filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 
<servlet-mapping> 
    <servlet-name>spring-mvc</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

而且,我弹簧security.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:security="http://www.springframework.org/schema/security" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/security 
http://www.springframework.org/schema/security/spring-security-4.0.xsd"> 


<beans:bean id="restAuthenticationEntryPoint" class="com.shakepoint.web.controller.rest.auth.RESTAuthenticationEntryPoint"/> 
<beans:bean id="restAuthenticationDeniedHandler" class="com.shakepoint.web.controller.rest.auth.RESTAccessDeniedHandler"/> 
<beans:bean id="restAuthenticationFailedHandler" class="com.shakepoint.web.controller.rest.auth.RESTAuthenticationFailedHandler"/> 
<beans:bean id="restAuthenticationSuccessHandler" class="com.shakepoint.web.controller.rest.auth.RESTAuthenticationSuccessHandler"/> 

<beans:bean id="successAuthenticationHandler" class="com.shakepoint.web.auth.SuccessAuthenticationHandler"/> 


<security:http create-session="stateless" 
       pattern="/rest/**" 
       use-expressions="true" 
       entry-point-ref="restAuthenticationEntryPoint" 
       authentication-manager-ref="authenticationManager" > 

    <!--<security:intercept-url pattern="/account/signin" access="permitAll()"/>--> 
    <security:intercept-url pattern="/rest/account/signup" access="permitAll()"/> 
    <security:intercept-url pattern="/rest/shop/**" access="hasRole('ROLE_MEMBER')"/> 

    <!--<security:http-basic />--> 
    <security:form-login login-processing-url="/rest/account/signin" 
         authentication-failure-handler-ref="restAuthenticationFailedHandler" 
         authentication-success-handler-ref="restAuthenticationSuccessHandler" 
         password-parameter="shakepoint_password" 
         username-parameter="shakepoint_username" 
         /> 
    <!--<security:access-denied-handler ref="restAuthenticationDeniedHandler"/>--> 
    <security:csrf disabled="true"/> 
</security:http> 



<security:http use-expressions="true" > 
    <security:intercept-url pattern="/tech/**" access="hasAnyRole('ROLE_TECHNICIAN')"/> 
    <security:intercept-url pattern="/admin/**" access="hasAnyRole('ROLE_ADMIN','ROLE_SUPER_ADMIN')"/> 
    <security:form-login 
     login-page="/signin" 
     login-processing-url="/login_to_checkpoint" 
     authentication-failure-url="/signin?error" 
     authentication-success-handler-ref="successAuthenticationHandler" 
     password-parameter="j_password" 
     username-parameter="j_email"/> 

    <security:logout logout-success-url="/" logout-url="/j_spring_security_logout"/> 
    <security:csrf/> 
</security:http> 

<security:global-method-security secured-annotations="enabled"/> 

在我AuthenticationSuccessHandler和AuthenticationFailedHandler,我只是发送应用/ JSON内容类型和包含的基本授权值,如果它是成功的对象,如果它失败的错误消息。

我成功登录到我的应用程序使用下面的Ajax请求

$.support.corps = true; 
     $.ajax({ 
      url: 'http://192.168.0.7:8080/rest/account/signin', 
      type: "POST", 
      data: JSON.stringify(signinData), 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (response) { 
       if(response.success == true){ 
        ... 
       } 
      }, 
      error: function(error){ 
       alert(error); 
      } 
     }); 

但是,当我要访问我的受保护的资源增加的基本授权头,它返回来自服务器的授权状态。 我也注意到春天正在响应中创建一个JSESSION cookie。

我UserDetailsS​​erivce工作正常,但是,真正的问题是:

我失去了任何头?或从我的服务器端项目的任何额外的设置?

请,任何帮助将充分认识

[更新] 这是Tomcat的7日志与春季安全

2015-10-07 12:16:18 DEBUG AntPathRequestMatcher:151 - Checking match of   request : '/rest/account/signin'; against '/rest/**' 
2015-10-07 12:16:18 DEBUG FilterChainProxy:324 - /rest/account/signin? shakepoint_password=tech2&[email protected] at position 1 of 9 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter' 
2015-10-07 12:16:18 DEBUG FilterChainProxy:324 - /rest/account/signin?shakepoint_password=tech2&[email protected] at position 2 of 9 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter' 
2015-10-07 12:16:18 DEBUG FilterChainProxy:324 - /rest/account/signin?shakepoint_password=tech2&[email protected] at position 3 of 9 in additional filter chain; firing Filter: 'HeaderWriterFilter' 
2015-10-07 12:16:18 DEBUG HstsHeaderWriter:128 - Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWr[email protected] 
2015-10-07 12:16:18 DEBUG FilterChainProxy:324 - /rest/account/signin?shakepoint_password=tech2&[email protected] at position 4 of 9 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter' 
2015-10-07 12:16:18 DEBUG AntPathRequestMatcher:151 - Checking match of request : '/rest/account/signin'; against '/rest/account/signin' 
2015-10-07 12:16:18 DEBUG UsernamePasswordAuthenticationFilter:211 - Request is to process authentication 
2015-10-07 12:16:18 DEBUG ProviderManager:162 - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider 
2015-10-07 12:16:18 DEBUG JdbcTemplate:634 - Executing prepared SQL query 
2015-10-07 12:16:18 DEBUG JdbcTemplate:569 - Executing prepared SQL statement [select email, password, role from user where email = ?] 
2015-10-07 12:16:18 DEBUG DataSourceUtils:110 - Fetching JDBC Connection from DataSource  
2015-10-07 12:16:18 DEBUG DriverManagerDataSource:162 - Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/shakepoint] 
2015-10-07 12:16:19 DEBUG DataSourceUtils:332 - Returning JDBC Connection to DataSource 
2015-10-07 12:16:19 DEBUG UsernamePasswordAuthenticationFilter:317 - Authentication success. Updating SecurityContextHolder to contain: org.springframew[email protected]511e4d66: Principal: [email protected]: Username: [email protected]; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_TECHNICIAN; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_TECHNICIAN 
2015-10-07 12:16:19 DEBUG SecurityContextPersistenceFilter:105 - SecurityContextHolder now cleared, as request processing completed 

但成功登录,II后尝试访问到我受保护的资源我得到了这个:

2015-10-07 12:22:09 DEBUG AntPathRequestMatcher:151 - Checking match of request : '/rest/shop/secured_ping'; against '/rest/**' 
2015-10-07 12:22:09 DEBUG FilterChainProxy:324 - /rest/shop/secured_ping at position 1 of 9 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter' 
2015-10-07 12:22:09 DEBUG FilterChainProxy:324 - /rest/shop/secured_ping at position 2 of 9 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter' 
2015-10-07 12:22:09 DEBUG FilterChainProxy:324 - /rest/shop/secured_ping at position 3 of 9 in additional filter chain; firing Filter: 'HeaderWriterFilter' 
2015-10-07 12:22:09 DEBUG HstsHeaderWriter:128 - Not injecting HSTS header since it did not match the requestMatcher org.springframework.se[email protected]7cb9682a 
2015-10-07 12:22:09 DEBUG FilterChainProxy:324 - /rest/shop/secured_ping at position 4 of 9 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter' 
2015-10-07 12:22:09 DEBUG AntPathRequestMatcher:131 - Request 'GET /rest/shop/secured_ping' doesn't match 'POST /rest/account/signin 
2015-10-07 12:22:09 DEBUG FilterChainProxy:324 - /rest/shop/secured_ping at position 5 of 9 in additional filter chain; firing Filter: 'DefaultLoginPageGeneratingFilter' 
2015-10-07 12:22:09 DEBUG FilterChainProxy:324 - /rest/shop/secured_ping at position 6 of 9 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter' 
2015-10-07 12:22:09 DEBUG FilterChainProxy:324 - /rest/shop/secured_ping at position 7 of 9 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter' 
2015-10-07 12:22:09 DEBUG AnonymousAuthenticationFilter:100 - Populated SecurityContextHolder with anonymous token: 'org.sprin[email protected]9055e4a6: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS' 
2015-10-07 12:22:09 DEBUG FilterChainProxy:324 - /rest/shop/secured_ping at position 8 of 9 in additional filter chain; firing Filter: 'ExceptionTranslationFilter' 
2015-10-07 12:22:09 DEBUG FilterChainProxy:324 - /rest/shop/secured_ping at position 9 of 9 in additional filter chain; firing Filter:  'FilterSecurityInterceptor' 
2015-10-07 12:22:09 DEBUG AntPathRequestMatcher:151 - Checking match of request : '/rest/shop/secured_ping'; against '/rest/account/signup' 
2015-10-07 12:22:09 DEBUG AntPathRequestMatcher:151 - Checking match of request : '/rest/shop/secured_ping'; against '/rest/shop/**' 
2015-10-07 12:22:09 DEBUG FilterSecurityInterceptor:218 - Secure object: FilterInvocation: URL: /rest/shop/secured_ping; Attributes: [hasRole('ROLE_MEMBER')] 
2015-10-07 12:22:09 DEBUG FilterSecurityInterceptor:347 - Previously Authenticated: org.sprin[email protected]9055e4a6: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS 
2015-10-07 12:22:09 DEBUG AffirmativeBased:65 - Voter: org.sp[email protected]629d991f, returned: -1 
2015-10-07 12:22:09 DEBUG ExceptionTranslationFilter:173 - Access is denied (user is anonymous); redirecting to authentication entry point 
org.springframework.security.access.AccessDeniedException: Access is denied 
at  org.springframework.security.access.vote.AffirmativeBased.decide(AffirmativeBased.java:83) 
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:232) 
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:123) 
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
at org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter.doFilter(DefaultLoginPageGeneratingFilter.java:162) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91) 
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213) 
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176) 
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344) 
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) 
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2522) 
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2511) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.lang.Thread.run(Thread.java:745) 
2015-10-07 12:22:09 DEBUG ExceptionTranslationFilter:202 - Calling Authentication entry point. 
2015-10-07 12:22:09 DEBUG SecurityContextPersistenceFilter:105 - SecurityContextHolder now cleared, as request processing completed 
+0

您可以添加日志记录信息吗?一般情况下,spring会在每次调用时打印登录调试模式。 –

+0

刚刚更新了我的问题 – AlbertoRuvel

回答

1

嗯,我发现我自己的答案这次,gett后荷兰国际集团到Spring文档时,我发现,春季安全需要BasicAuthorizationProcessingFilter设置应用

我做的唯一的事情就是SecurityContext的:

添加认证过滤器

<beans:bean id="basicAuthenticationFilter" class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter"> 
    <beans:constructor-arg index="0" ref="authenticationManager"/> 
    <beans:constructor-arg index="1" ref="restAuthenticationEntryPoint"/> 
</beans:bean> 

改变了我的http元素添加一个自定义筛选器子项

<security:http create-session="stateless" 
       pattern="/rest/**" 
       use-expressions="true" 
       entry-point-ref="restAuthenticationEntryPoint" 
       authentication-manager-ref="authenticationManager" > 

    <!--<security:intercept-url pattern="/account/signin" access="permitAll()"/>--> 
    <security:intercept-url pattern="/rest/account/signup" access="permitAll()"/> 
    <security:intercept-url pattern="/rest/shop/**" access="hasAnyRole('ROLE_MEMBER', 'ROLE_SUPER_ADMIN', 'ROLE_ADMIN', 'ROLE_TECHNICIAN')"/> 
    <security:custom-filter ref="basicAuthenticationFilter" position="BASIC_AUTH_FILTER"/> 
    <security:form-login login-processing-url="/rest/account/signin" 
         authentication-failure-handler-ref="restAuthenticationFailedHandler" 
         authentication-success-handler-ref="restAuthenticationSuccessHandler" 
         password-parameter="shakepoint_password" 
         username-parameter="shakepoint_username" 
         /> 
    <!--<security:access-denied-handler ref="restAuthenticationDeniedHandler"/>--> 
    <security:csrf disabled="true"/> 
</security:http> 

经过这些更改,我的应用程序安全性开箱即用!