Spring安全性提供了一种保护页面和重定向用户访问被拒绝页面的好方法。我希望使用弹簧安全性做的是,如果用户试图访问页面A并且他无权访问,我希望用户留在同一页面上以查看所有相关的菜单项(菜单项给出了一个如果他们购买该权利他们会得到什么的想法)同时我想告诉用户访问被拒绝,他们将需要购买该频道。
我可以通过<security:authorize
标记执行此操作,但由于应用程序中的内容组织方式,我希望在网址级别执行此操作。
答案 0 :(得分:0)
我们为我们的产品做了类似的事情
Authentication authentication = rememberMeServices.createSuccessfulAuthentication(request,
userDetails);
2.创建这些apis以在jsp页面中使用
public static boolean isFullyAuthenticated() {
SecurityContext securityContext = SecurityContextHolder.getContext();
if (securityContext != null) {
Authentication authentication = securityContext.getAuthentication();
return isFullyAuthenticated(authentication);
}
return false;
}
public static boolean isFullyAuthenticated(Authentication authentication) {
return !authenticationTrustResolver.isAnonymous(authentication) && !authenticationTrustResolver
.isRememberMe(authentication);
}
其中authenticationTrustResolver由spring提供 AuthenticationTrustResolver authenticationTrustResolver = new AuthenticationTrustResolverImpl();
<%if(isFullyAuthenticated()){%>
//Menu option with links
<%}else{%>
//menu option which shows popup/ msgs
<%}%>
注意:实用方法是明确编写的,因为我们无法为rememberme /匿名用户找到安全标记
答案 1 :(得分:0)
我认为你应该实现自定义org.springframework.security.web.access.AccessDeniedHandler
。每当用户尝试访问未授权访问的页面时,都会抛出该页面。在handle
方法中,您将HttpServletRequest
作为参数接收,您可以使用该方法对每个请求端点进行特定处理。
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException)
throws IOException, ServletException {
String errorPage = doSomethingWithRequestUri(request);
response.sendRedirect(errorPage);
}
但是你必须非常小心,因此如果你试图重定向到用户试图输入的同一个网址,你将陷入无限重定向循环。也许你应该使用授权/未经授权的模式,例如:
您可能已经考虑过这个问题并且已经实现了解决方案......所以在AccessDeniedHandler
实现中您应该只关注授权/未经授权的网址关系