spring - Spring MVC 中的安全 RESTful Web 方法

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

我有某种 Controller :

    @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 上应用此处理程序?

最佳答案

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

这是我的安全配置的片段。

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

关于spring - Spring MVC 中的安全 RESTful Web 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9192885/

相关文章:

java - Bean创建异常: No factory method found

java - 是否可以在 spring-boot 中更改/health 端点?

java - 只从 MOQUI FrameWork 中的 restServices 返回实体的某些字段?

spring - ApplicationEventMulticaster 未使用 Spring HATEOAS 初始化

java - Spring + hibernate : Error bean creation

java - 无法将 DWR 与 Spring 合并

maven - 使用 glassfish-embedded-all 和 arquillian-glassfish-embedded-3.1 Artifact 执行 maven 安装时出错

java - Pylons 或 TurboGears 与 .NET 或 Java

java - 如何使用 Spring MVC/Spring Boot 编写适当的全局错误处理程序

xml - 为 json 和 xml 的 Restful 端点设置 Spring MVC