【发布时间】:2016-04-21 11:41:05
【问题描述】:
我使用 Spring 4 和 Spring Security 4 来保护 Web 服务。一个普通的网络服务在没有安全性的情况下运行良好。我的安全 Web 服务在本地和单元测试中似乎也能很好地工作。
我们使用的是 SiteMinder 示例,这意味着我们有来自远程授权的身份验证。这会在我们登录时向浏览器传递一个令牌。我们通过请求标头传递身份验证令牌,这是从 customerUserDetailsService 中提取的。该类和方法从标头中提取令牌,根据该远程权限对用户进行身份验证,然后我们得到一个用户名。通过该用户名,我们对数据库进行 DAO 调用以获取用户详细信息及其角色,在 Spring Security Context 中,它们使用角色来授予权限。这一切都很好,我们得到了一个经过身份验证的用户,我们有他们的角色/grantedAuthorities。
因此,如前所述,我们现在只是使用 spring-security.xml 保护 Web 服务,以根据用户的角色保护 Web 服务。同样,这一切似乎都适用于单元测试。我们有一个无法访问网站的用户的令牌,我们正确地返回了 403 错误。当我们为具有正确角色的用户使用令牌时,能够执行网络服务。
现在我正在尝试将其部署到新环境中,但运气不佳。
所以,我有一个 spring-security.xml,看起来像:
<http use-expressions="true" auto-config="false" entry-point-ref="http403EntryPoint">
<!-- Additional http configuration omitted -->
<intercept-url pattern="/records/authorizedRecords" access="hasRole('portalUser')" />
<intercept-url pattern="/records/myCode" access="hasRole('portalUser')" />
<intercept-url pattern="/users/email" access="hasRole('appAdmin')" />
<custom-filter position="PRE_AUTH_FILTER" ref="openAmFilter" />
</http>
<beans:bean id="openAmFilter" class="org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter">
<beans:property name="principalRequestHeader" value="openam_token"/>
<beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>
<beans:bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
<beans:property name="preAuthenticatedUserDetailsService">
<beans:bean id="userDetailsServiceWrapper" class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
<beans:property name="userDetailsService" ref="customUserDetailsService"/>
</beans:bean>
</beans:property>
</beans:bean>
<authentication-manager alias="authenticationManager">
<authentication-provider ref="preauthAuthProvider" />
</authentication-manager>
<beans:bean id="customUserDetailsService" class="com.agmednet.server.security.CustomUserDetailsService"></beans:bean>
<beans:bean id="http403EntryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint">
当我们尝试访问时:
<intercept-url pattern="/users/email/*" access="hasRole('appAdmin')" />
这似乎不匹配:
/rest/users/email/myemail@someemail.com
所以我把它改成:
<intercept-url pattern="/rest/users/email/*" access="hasRole('appAdmin')" />
我从日志中得到这个。
DEBUG: org.springframework.security.web.util.matcher.AntPathRequestMatcher - Checking match of request : '/rest/trials/integratedtrials'; against '/rest/users/email/*'
DEBUG: org.springframework.security.web.access.intercept.FilterSecurityInterceptor - Public object - authentication not attempted
DEBUG: org.springframework.security.web.FilterChainProxy - /rest/trials/integratedTrials reached end of additional filter chain; proceeding with original chain
DEBUG: org.springframework.security.web.access.ExceptionTranslationFilter - Chain processed normally
DEBUG: org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
此时,我知道我们有一个经过身份验证的用户、用户名和角色都列在日志中。我们似乎在 spring-security.xml 中找到了匹配的 URL,URL 匹配,角色匹配,现在我觉得我们应该执行 web-service 背后的逻辑,但我收到一条错误消息:
Apache Tomcat/8.0.30 - 错误报告 /services/rest/users/email/myemail@someemail.com
请求的资源不可用。
我在这里绝对感到震惊....不安全的网络服务工作得很好。我必须错过一些东西吗?网址中是否带有“休息”一词? Web 服务在不安全的情况下总是有效的。我添加了安全性并添加了单元测试来测试这一切,现在我不确定发生了什么?
【问题讨论】:
标签: web-services rest spring-mvc spring-security