【发布时间】:2014-07-20 02:18:03
【问题描述】:
我的问题很简单“在使用 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: Access is denied
我配置为this 准确回答但仍然出现错误。如果我错了请纠正我“我认为这个问题可能是由于 gwt 的 RPC”因为非 rpc 方法很好并且重定向到我的 unSecure.html 。我花了大约 3 天的时间来解决这个错误。在 onFailure(Throwable catch) 我的异步方法展示
500 服务器调用失败;详情见服务器日志
我想展示我的配置。
spring-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。我真的很感激你的任何建议。对不起我的长问题。我不想再撞我的头了!谢谢。
【问题讨论】:
-
我还使用了 Spring Security 的登录安全性,正如我所描述的,效果很好。但是对于方法级别的安全性,我做错了什么?这不会重定向到我的 unSecure.html 页面,除非以
AccessDenied身份登录控制台。
标签: java spring gwt spring-security gwt-rpc