2012-02-08 17 views
2

私は、コントローラのいくつかの種類があります:私は春のセキュリティを使用してい春のMVCでセキュアなRESTfulなWebメソッド

@Controller 
    public class DefaultController { 

    @RequestMapping(value="/index.html", method=RequestMethod.GET) 
     public String indexView(){ 
      return "index"; 
     } 

    @RequestMapping(value="/some.action", method=RequestMethod.POST) 
    @ResponseBody 
     public MyObject indexView(some parametrs....){ 
      MyObject o= daoService.getO(id); 
        return o; 
     } 
} 

<security:global-method-security secured-annotations="enabled" /> 
<security:http auto-config="true" access-denied-page="/accessDenied.jsp"> 
    <security:form-login login-page="/login.html" login-processing-url="/login" authentication-failure-url="/login.html?login_error=1" default-target-url="/"/> 
    <security:http-basic/> 
    <security:intercept-url pattern='/**' access='ROLE_USER' /> 
    <security:logout logout-url="/logout" logout-success-url="/"/> 
    <security:remember-me services-ref="rememberMeServices"/> 
    </security:http> 

、私の問題は、次の番目です:

を認証されていないユーザがいないAJAXを使用して/some.actionにアクセスすると、Spring Securityは301コマンド(アクセス拒否ページにリダイレクト)を返します。

私が必要とするのは、ユーザーが200 OKを返し、認証エラーメッセージをクライアントまたはイベントに送信した場合でも、最悪の場合は400何らかのエラーを返す場合です。

カスタム認証成功ハンドラを作成する必要があることを理解していますが、それを行うことはできますか?このハンドラを* .action URIにどのように適用できますか?

+0

btw wouldntは正しいコードを返すでしょうか? http://stackoverflow.com/questions/3297048/403-forbidden-vs-401-unauthorized-http-responses – flurdy

+0

@flurdy、yes 401は理にかなっています。 –

+0

これはhttp://www.byteclip.com/spring-security-post-authentication-logic/に役立つかもしれません。この方法でhttp応答コードを変更できます –

答えて

2

AJAX認証では、カスタムセキュリティエントリポイントrefを追加して、ユーザーが認証されているかどうかを確認しました。そうでない場合、私はそれらに4xxエラーコードを送ります。その後、私のAjax呼び出しで、エラーが返されたかどうかを確認し、そうであれば、それらをログインページにリダイレクトします。

ここに私のsecurity-configのスニペットがあります。

<security:http entry-point-ref="myAuthenticationEntryPoint" auto-config="true" use-expressions="true"> 
... 
... 
</security:http> 
<bean id="myAuthenticationEntryPoint" class="com.security.AjaxAuthenticationEntryPoint" > 
     <property name="loginFormUrl" value="/login"/> 
</bean> 

は、ここに私のカスタムのエントリポイントです:

public class AjaxAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint 
{ 
    @Override 
    /** 
    * This method only gets call when the user logs out or when the session is invalid 
    * 
    * It checks to see if the request is an ajax request 
    * if so then return an error 
    * else then do the natural check 
    */ 
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) 
      throws IOException, ServletException 
    {      
     if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) 
     { 
      if (request.getSession() != null) 
      { 
       Object targetUrl = request.getSession().getAttribute(WebAttributes.SAVED_REQUEST); 
       if (targetUrl != null) 
       {     
        response.sendError(HttpServletResponse.SC_EXPECTATION_FAILED);          
       } 
      } 
     } 
     else 
     { 
      super.commence(request, response, authException); 
     } 

    } 
} 

はここに私のjQueryのコールの抜粋ですが、リロードは、ログインページが表示されるようになります。

error: function (xhr, textStatus, errorThrown) 
       {  
        // 417 is sent from the server to indicate that 
        // page needs to be reloaded 
        // 
        if (xhr.status == 417) 
        { 
         xhr = null; 
         window.location.reload();      
        } 
       } 
関連する問題