spring security - access-denied-handler

时间:2013-10-22 12:13:14

标签: java spring spring-mvc spring-security

我在使用Spring Security时遇到了一些问题,并且使访问被拒绝处理程序无法正常工作。

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

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

弹簧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>

LoginController.java     import 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

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

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

您是否也访问了管理员页面,其中包含没有管理员权限的正确登录信息,或者您在没有登录的情

<强> Editted:

  1. 您是否可以通过添加一些日志语句或调试来检查当您使用正确的登录(没有admin priv)访问/ admin时,是否调用了accessDenied()方法?
  2. 您也可以共享您的登录jsp,您可能没有正确显示消息参数
  3. 当用户没有必需的priv但是有效用户然后调用accessDeniedHandler时,你可以在调试日志中看到类似的东西
  4.   

    访问被拒绝(用户不是匿名的);委托给   AccessDeniedHandler

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

答案 1 :(得分:1)

使用Spring ExceptionTranslationFilter将帮助您获得所需的输出。你可以尝试一下

添加与您的所有请求匹配的过滤器

然后按如下方式注入ExceptionTranslationFilter

<bean id="exceptionTranslationFilter" class="org.springframework.security.web.access.ExceptionTranslationFilter">
    <property name="authenticationEntryPoint" ref="authenticationEntryPoint" />
    <property name="accessDeniedHandler">
        <bean class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
            <property name="errorPage" value="/login/denied.action"/>
        </bean>
    </property>
</bean>

答案 2 :(得分:0)

简单的方法

三个步骤 1-

<块引用>

.exceptionHandling().accessDeniedPage("/access-denied")

2-

    @RequestMapping("/access-denied")
public void access(HttpServletResponse response  ,  HttpServletRequest request) throws IOException {
    response.sendRedirect(request.getContextPath() +"?accessDenied");
}

3-

<c:if test="${param.accessDenied != null }">
<i class="failed">access denied  .</i>                                  
</c:if>