【问题标题】:Spring Security - logout and access control not workingSpring Security - 注销和访问控制不起作用
【发布时间】:2016-09-30 23:29:56
【问题描述】:

所以我的 spring 配置 xml 文件中有这个。

<http auto-config="true" use-expressions="true">

    <intercept-url pattern="/welcome/*" access="hasRole('ADMIN')" />

    <!-- <intercept-url pattern="/login" requires-channel="https" /> -->

    <!-- access denied page -->
    <access-denied-handler error-page="/403" />

    <form-login login-page="/login" 
        default-target-url="/welcome"
        authentication-failure-url="/login?error" 
        username-parameter="emailId"
        password-parameter="pwd" />
    <logout logout-success-url="/login?logout"/>
</http>

角色在登录时已正确验证。我有两个问题:

  1. pattern="/welcome/*"pattern="/welcome*"pattern="/welcome/**" 有什么区别?当pattern="/welcome/*"时,登录成功,用户看到页面。在其他两个选项中,都会出现 403 Access Denied 页面。用户确实拥有“管理员”权限)
  2. Spring 安全进程如何注销?我的welcome.jsp 文件中有以下代码:

    <c:url value="/logout" var="logoutUrl" />
    <form action="${logoutUrl}" method="GET" id="logoutForm">
        <input type="hidden" name="${_csrf.parameterName}"
        value="${_csrf.token}" />
    </form>
    <script>
    function formSubmit() {
    document.getElementById("logoutForm").submit();
    }
    </script>
    
    <c:if test="${pageContext.request.userPrincipal.name != null}">
        <h2>
        User : ${pageContext.request.userPrincipal.name} | <a
        href="javascript:formSubmit()"> Logout</a>
        </h2>
    </c:if>
    

    这在我的控制器中:

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logoutPage(HttpServletRequest request,     HttpServletResponse response) {
        Authentication auth =     SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return "redirect:/login?logout";
    }
    

    页面正确重定向并显示“注销成功”页面,但如果我将 URL 更改为再次转到“/welcome”,它会显示该页面。它不应该显示 403 - Access Denied 页面吗?

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    关于 Spring Security 中的 Ant 路径匹配器

    您提到的使用 Ant 样式语法的主要作用是确定哪些路径是有效的。

    映射使用以下规则匹配 URL:

    • ? 匹配一个字符
    • * 匹配零个或多个字符
    • ** 匹配路径中的零个或多个目录

    关于您的案例:

    • /welcome/* - 这可能对像 /welcome/hello/welcome/#hello/welcome/?abc=123 这样的 URL 有效
    • /welcome* - 有效为 /welcome?abc=123/welcome#abc=123
    • /welcome/** - 有效大小写为 /welcome/hello/bye?abc=123

    更多信息请访问Spring Documentation

    注销操作

    我假设您使用 xml-configuration 来确保安全。无论如何,这可以修改为使用纯 Java 配置。

    app-security.xml 中应该是这样的:

    <http use-expressions="true"
          disable-url-rewriting="true">
    
        <http-basic />
    
        <!-- other configurations -->
    
        <intercept-url pattern="/login**" access="isAnonymous()"/>
        <intercept-url pattern="/**" access="isAuthenticated()"/>
    
        <!-- other configurations -->
        <logout logout-url="/logout"
                logout-success-url="/login"/>
    </http>
    

    index.html 文件中的某处:

    <a href="<c:url value="/logout" />" id="item-btn-logout">
        <i class="icon-off"></i> Logout
    </a>
    

    最重要的部分是 URL:/logout

    【讨论】:

      猜你喜欢
      • 2011-10-05
      • 2014-10-15
      • 2013-05-24
      • 1970-01-01
      • 2011-08-20
      • 2018-06-23
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多