具有Spring角色基础安全性的GWT RPC

时间:2014-05-30 10:40:07

标签: java spring gwt spring-security gwt-rpc

我的问题非常简单" 使用Spring安全性和GWT RPC时会遇到什么问题?"

我想在GWT的RPC方法上使用spring的方法级别安全性。例如:在我的ServiceImpl类中,我使用了Expression-Based Access Control,如下所示。

@PreAuthorize("hasRole('ROLE_ADMIN')")
public final String getById(Long id) {
    .........
}

如果取消授权角色访问用户尝试访问处理此rpc方法的页面,则会抛出异常并且不会重定向到我的访问被拒绝页面。我不知道为什么不进入我拒绝访问的页面?

我的控制台出现异常
  

引发意外异常:org.springframework.security.access.AccessDeniedException:访问被拒绝

我确切地将this答案配置为但仍然高于错误。如果我错了,请纠正我" 我认为这个问题可能是由于gwt的RPC "因为非rpc方法很好并且重定向到我的 unSecure.html 。我花了大约3天的时间来处理这个错误。在我的异步方法show onFailure(Throwable caught)

  

500服务器上的呼叫失败;有关详细信息,请参阅服务器日志

我想显示我的配置。


弹簧security.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:sec="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">

<sec:global-method-security
    secured-annotations="enabled" pre-post-annotations="enabled" />
<sec:http auto-config="false" entry-point-ref="authenticateFilterEntryPoint">
    <sec:access-denied-handler ref="accessDeniedHandler" />
    <sec:intercept-url pattern="/login.html" />

    <sec:logout logout-url="/logout.html" logout-success-url="/login.html"
        invalidate-session="true" />
    <sec:form-login login-page="/login.html"
        login-processing-url="/login_check" authentication-failure-url="/login.html?error=1" />

    <sec:session-management invalid-session-url="/login.html">
        <sec:concurrency-control max-sessions="50"
            error-if-maximum-exceeded="true" />
    </sec:session-management>
    <sec:remember-me key="mykey"
        token-validity-seconds="604800" />
</sec:http>

<beans:bean id="authenticateFilterEntryPoint"
    class="mypackage.common.security.SessionTimeoutEntryPoint">
    <beans:property name="loginFormUrl" value="/login.html" />
</beans:bean>

<beans:bean id="accessDeniedHandler"
    class="mypackage.common.security.AccessDeniedEntryPoint">
    <beans:property name="errorPage" value="/unSecure.html" />
</beans:bean>

<beans:bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <beans:property name="defaultErrorView" value="uncaughtException" />
    <beans:property name="excludedExceptions"
        value="org.springframework.security.access.AccessDeniedException" />

    <beans:property name="exceptionMappings">
        <beans:props>
            <beans:prop key=".DataAccessException">dataAccessFailure</beans:prop>
            <beans:prop key=".NoSuchRequestHandlingMethodException">resourceNotFound</beans:prop>
            <beans:prop key=".TypeMismatchException">resourceNotFound</beans:prop>
            <beans:prop key=".MissingServletRequestParameterException">resourceNotFound</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

<beans:bean id="authenticationUserService"
    class="mypackage.common.security.AuthenticationUserService" />

<sec:authentication-manager>
    <sec:authentication-provider
        user-service-ref="authenticationUserService">
        <sec:password-encoder hash="md5" />
    </sec:authentication-provider>
</sec:authentication-manager>

<beans:bean id="authLoggerListener"
    class="org.springframework.security.authentication.event.LoggerListener" />
<beans:bean id="eventLoggerListener"
    class="org.springframework.security.access.event.LoggerListener" />

AccessDeniedEntryPoint.java

public class AccessDeniedEntryPoint extends org.springframework.security.web.access.AccessDeniedHandlerImpl {
private static final Logger logger = LoggerFactory.getLogger(AccessDeniedEntryPoint.class);

@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
        AccessDeniedException accessDeniedException) throws IOException, ServletException {
    super.handle(request, response, accessDeniedException);

}
}

SessionTimeoutEntryPoint.java

public class SessionTimeoutEntryPoint extends LoginUrlAuthenticationEntryPoint {

@Override
public final void commence(final HttpServletRequest request, final HttpServletResponse response,
        final AuthenticationException authException) throws IOException, ServletException {
    super.commence(request, response, authException);
}
}

因此,当取消授权角色用户访问此方法时,我希望获得 unSecure.html 。我真的很感激你的任何建议。对不起我的长话题。我不想再打我的脑袋了!感谢。

1 个答案:

答案 0 :(得分:2)

正如我上次的回复,我很久以前就用过这个。在我的情况下,而不是总是处理url我委托给处理程序。

我的意思是,如果你想将GWT-RPC与Spring-Security集成,我做的第一件事就是从我的GWT应用程序尝试登录Spring。

因此,您需要做的第一件事就是为登录创建一个RPC-CALL (here official documentation)

我发现注释@RemoteServiceRelativePath("examplelogin.rpc")很有用,所以如果您使用它,您可以利用路径,然后在spring-security.xml中您可以按这些路径过滤请求(请参阅更新的示例)下文)。

不确定为什么要将GWT-RPC与Spring-Security集成,然后你指定/login.html(它应该是一个rpc调用,之前描述的而不是html?但是你可能正在使用自己的MVP和GWT,所以我并不是说这是错的,只是让我感到惊讶:))。

阅读你的代码我没有看到错误,但在我的情况下,我有一些不同之处:

....
<http use-expressions="true" entry-point-ref="http401UnauthorizedEntryPoint">

    <intercept-url pattern="/yourProject/public.rpc" access="permitAll" />
    <intercept-url pattern="/yourProject/examplelogin.rpc" access="hasRole('ROLE_ADMIN')" />

    <form-login authentication-success-handler-ref="authenticationSuccessHandler"
        authentication-failure-handler-ref="authenticationFailureHandler"/>


    ..... //logout, session-management, custom-filters....

    <beans:bean id="http401UnauthorizedEntryPoint" 
                class="your.project.Http401UnauthorizedEntryPoint" />
    <beans:bean id="authenticationSuccessHandler" 
                class="your.project.GWTAuthenticationSuccessHandler"/>
    <beans:bean id="authenticationFailureHandler" 
                class="your.project.GWTAuthenticationFailureHandler"/>
</http>
....

您具体询问了将Spring-SecurityGWT-RPC一起使用的可能性(这是因为<intercept-url>代码中我放置了.rpc网址)

请注意

答案是肯定的,我已经完成了(三年前,但我做了:))

我认为你的问题的关键是:

  • 指定RPC调用的拦截器url或/login.html

  • 像我一样代表处理程序(小心,也许你做得很好 并且错误在其他方面,但至少我在示例中确实喜欢它 工作)。

很抱歉没有直接向您显示错误,我希望这些答案会有所帮助。

感谢。

相关问题