【问题标题】:Secure RESTful Web Method in Spring MVCSpring MVC 中的安全 RESTful Web 方法
【发布时间】:2012-03-01 04:14:24
【问题描述】:

我有某种控制器:

    @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; 
        }
}

我正在使用 Spring Security:

<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?

【问题讨论】:

标签: spring jakarta-ee spring-mvc spring-security


【解决方案1】:

对于 AJAX 身份验证,我添加了一个自定义安全入口点 ref 以检查用户是否已通过身份验证。如果不是,我会向他们发送 4xx 错误代码。然后在我的 Ajax 调用中,我检查是否返回了错误,如果是,我将它们重定向到我的登录页面。

这是我的安全配置的 sn-p。

<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 调用的 sn-p,重新加载会导致登录页面出现。

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();                       
                    }
               }

【讨论】:

    猜你喜欢
    • 2012-01-21
    • 2014-01-02
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 2012-03-13
    • 2015-02-20
    相关资源
    最近更新 更多