2014-10-03 152 views
0

我有一个现有的网络应用程序,被春季安全覆盖。 我需要添加一个servlet(或一般端点),其工作原理是这样的:Spring Security Servlet登录

  • 它收到一个JSON
  • 它处理数据
  • 进行认证
  • 一个POST返回响应与其他数据。

我读过有关春季安全(http://docs.spring.io/spring-security/site/docs/3.0.x/reference/springsecurity.html)的文件,但我对如何以及在何处执行我的代码有点混乱。我正在考虑在阶段4中使用自定义过滤器:UsernamePasswordAuthenticationFilter。但是,我并不确定,因为如果我正确理解框架,过滤器将应用于每个请求,并且我只需登录一次,然后接受为已认证。

过滤器是正确的方法还是有更好的方法?

回答

0

在编写Web服务时,最好每个请求重新授权客户端。这意味着客户端必须每次都传递其凭据(用户名/密码)。

顺便说一句,如果你正在编写一个web服务,那么最好使用基于ssl的BasicAuthenticationEntryPoint而不是UsernamePasswordAuthenticaitonFilter。

但是,为了充分回答您的问题,我们使用了安全/ http模式匹配器来实现这一诀窍。

<security:http pattern="/api/**" create-session="stateless" access-decision-manager-ref="unanimousBased" entry-point-ref="authenticationEntryPoint" > 
    <security:intercept-url pattern="/api/**" access="IS_AUTHENTICATED_FULLY,group_User_role_default" /> 
    <security:custom-filter ref="basicAuthenticationFilter" after="BASIC_AUTH_FILTER" /> 
</security:http> 

<bean id="basicAuthenticationFilter" class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter"> 
    <property name="authenticationManager" ref="authenticationManager" /> 
    <property name="authenticationEntryPoint" ref="authenticationEntryPoint" /> 
</bean> 

<bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint"> 
    <property name="realmName" value="api" /> 
</bean> 

最后,通常在处理数据之前首先进行授权。看起来很奇怪,数据将被处理,然后检查授权以查看是否可以发送响应,但也许这只是我。

+0

“处理数据”我的意思是代码解析json并查询数据库以检查凭据。 如果我没有正确地理解,你建议使用映射到我的servlet/service的url上的BasicAuthenticationEntryPoint,对吧? – 2014-10-09 08:42:31