Java 如果用户未授权,则Spring Security显示404
我的spring安全配置是Java 如果用户未授权,则Spring Security显示404,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我的spring安全配置是 <http pattern="/resources/**" security="none"/> <http pattern="/login**" security="none"/> <http pattern="/invalidsession**" security="none"/> <http pattern="/forgotpassword**" security="none"/> &
<http pattern="/resources/**" security="none"/>
<http pattern="/login**" security="none"/>
<http pattern="/invalidsession**" security="none"/>
<http pattern="/forgotpassword**" security="none"/>
<http pattern="/accessdenied**" security="none"/>
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/common/**" access="hasAnyRole('SITE_ADMIN','CLIENT_ADMIN')" />
<intercept-url pattern="/site/**" access="hasRole('SITE_ADMIN')" />
<intercept-url pattern="/**" access="hasRole('CLIENT_ADMIN')" />
<!-- access denied page
<access-denied-handler error-page="/accessdenied" />
-->
<access-denied-handler ref="my403" />
<form-login login-page="/login"
username-parameter="username"
password-parameter="password" authentication-failure-url="/login?error"
authentication-success-handler-ref="myAuthenticationSuccessHandler"
/>
<logout logout-success-url="/login?logout" />
<session-management invalid-session-url="/invalidsession" />
</http>
当SITE_ADMIN用户打开一个不存在的页面时,它会显示拒绝访问页面,而不是404页面。这是因为
。Spring security正在检查pagenotfound之前的授权。如果页面不存在且站点管理员当前正在登录,我如何显示404。不确定您为什么要这样做,但您可以通过实现AuthenticationEntryPoint
来完成,并为您的客户端管理员设置开始方法404
,并为其他人保留默认值
差不多
@Component
public class EntryPointUnauthorizedHandler implements AuthenticationEntryPoint{
@Override
public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,AuthenticationException e) throws IOException, ServletException {
// use SecurityContextHolder.getContext() for your security checks
httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED,"Access Denied");
}
}
现在您可以在http安全配置中添加入口点了这一要求将很难满足:Spring安全过滤器在dispatcher servlet看到请求之前进行web授权测试
那么我们能做些什么呢?这里有一些路径,但我从未尝试过
1/使用自定义的AccessDeniedHandler
。它的handle方法将获得请求和响应,因此它可以尝试使用虚拟响应将它们直接传递给DispatcherServlet。困难的部分是分析响应,看看它是否是404错误页面,如果它是复制真实响应中的虚拟响应,否则继续使用默认的AccessDeniedHandlerImpl
方法。看起来有点黑
2/放弃web安全,只在所有服务方法上一致使用方法安全。这样,仅当DispatcherServlet成功找到请求的控制器时,才会调用安全性。缺点是静态页面没有安全性。但这条路如果更像春天的路
不管怎么说,我决不会那样做。如果有人不允许进入特定的层次结构,我不希望他确切知道存在哪些页面。我并不特别喜欢模糊处理的安全性,但我更喜欢隐藏对用户不直接有用的所有内容。谢谢您的评论。我把这件事告诉了我的经理,他说不要这样做。