gpt4 book ai didi

java - spring security - 拒绝访问处理程序

转载 作者:搜寻专家 更新时间:2023-10-31 20:12:21 24 4
gpt4 key购买 nike

我在使用 Spring Security 和让拒绝访问处理程序工作时遇到了一些问题。

Spring Security 正在运行,但是当我在没有所需权限 (ROLE_ADMIN) 的情况下访问/admin 时,Spring Security 只是重定向到根页面,这是我的登录表单页面。

我希望能够将用户重定向到/accessdenied 或/?accessdenied=true 这应该加载登录页面并显示以下消息:“权限被拒绝 - 请登录”

spring-security.xml:

<beans:beans 
xmlns:security="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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">


<security:http>
<security:intercept-url pattern='/home' access='ROLE_USER,ROLE_ADMIN' />
<security:intercept-url pattern='/admin*' access='ROLE_ADMIN' />
<security:form-login login-page='/' default-target-url='/home' authentication-failure-url='/?error=true' />
<security:logout logout-success-url='/' />
<security:access-denied-handler error-page="/accessdenied"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name='a' password='a' authorities='ROLE_ADMIN,ROLE_USER' />
<security:user name='u' password='u' authorities='ROLE_USER' />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans:beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/spring-security.xml
</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>

</web-app>

登录 Controller .java 导入 java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model)
{
logger.info("Welcome home! The client locale is {}.", locale);

return "login";
}

@RequestMapping(value = "/accessdenied", method = RequestMethod.GET)
public String accessDenied(Locale locale, Model model)
{
logger.info("Welcome home! The client locale is {}.", locale);

model.addAttribute("message", "Permission denied - please login");

return "login";
}
}

我已经尝试了几个指南,包括以下,但都没有用:

http://www.mkyong.com/spring-security/customize-http-403-access-denied-page-in-spring-security/ access denied page using spring security not working How to redirect to access-denied-page with spring security

如有任何帮助,我们将不胜感激。

最佳答案

您能否检查日志并确认在您的情况下抛出 AuthenticationException 或在您访问/admin 页面时抛出 AccessDeniedException。

您还访问了没有管理员权限的正确登录名的管理页面,或者您根本没有登录就访问了它?

已编辑:

  1. 您能否通过添加一些日志语句来检查或调试当您使用正确的登录名(没有管理员权限)访问/admin 时,是否调用了 accessDenied() 方法?
  2. 你也可以分享你的登录jsp,你可能没有正确显示消息参数
  3. 当用户没有所需的 priv 但是是有效用户时,将调用 accessDeniedHandler,您可以在调试日志中看到类似的内容

access is denied (user is not anonymous); delegating to AccessDeniedHandler

AccessDeniedHndler 会将请求转发到 errorPage(不是重定向)

关于java - spring security - 拒绝访问处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19517564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com