sec:authorize access =“hasRole('ROLE_USER')”不起作用

时间:2017-06-11 14:24:06

标签: spring spring-mvc spring-security jspx

我的menu.jspx无法正常工作。我可以看到< sec:authorize access

        <sec:authorize access="hasRole('ROLE_USER')">
            <a href="${addContactUrl}"><h3>${menuAddContact}</h3></a><!--будет отображатсья только если юзер зашел и нужной ролью-->
        </sec:authorize>

        <sec:authorize access="isAnonymous()"><!--Если не зашел то отбражается форма входа-->
            <div id="login">
                <form name="loginForm" action="${loginUrl}" method="post">
                    <table>
                        <caption align="left">Login:</caption>
                        <tr>
                            <td>User Name:</td>
                            <td><input type="text" name="j_username"/></td>
                        </tr>
                        <tr>
                            <td>Password:</td>
                            <td><input type="password" name="j_password"/></td>
                        </tr>
                        <tr>
                            <td colspan="2" align="center"><input type="submit"
                                                                  name="submit"
                                                                  value="Login"/></td>
                        </tr>
                    </table>
                </form>
            </div>



<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div id="menu" xmlns:jsp="http://java.sun.com/JSP/Page"
     xmlns:c="http://java.sun.com/jsp/jstl/core"
     xmlns:spring="http://www.springframework.org/tags"
     xmlns:sec="http://www.springframework.org/security/tags"
     version="2.0">
    <jsp:directive.page contentType="text/html;charset=UTF-8" />
    <jsp:output omit-xml-declaration="yes" />

    <spring:message code="menu_header_text" var="menuHeaderText"/>
    <spring:message code="menu_add_contact" var="menuAddContact"/>
    <spring:url value="/contacts?form" var="addContactUrl"/>

    <spring:message code="label_login" var="labelLogin"/>
    <spring:url var="loginUrl" value="/j_spring_security_check"/>

    <h3>${menuHeaderText}</h3>
    <sec:authorize access="hasRole('ROLE_USER')">
        <a href="${addContactUrl}"><h3>${menuAddContact}</h3></a><!--будет отображатсья только если юзер зашел и нужной ролью-->
    </sec:authorize>

    <sec:authorize access="isAnonymous()"><!--Если не зашел то отбражается форма входа-->
        <div id="login">
            <form name="loginForm" action="${loginUrl}" method="post">
                <table>
                    <caption align="left">Login:</caption>
                    <tr>
                        <td>User Name:</td>
                        <td><input type="text" name="j_username"/></td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><input type="password" name="j_password"/></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit"
                                                              name="submit"
                                                              value="Login"/></td>
                    </tr>
                </table>
            </form>
        </div>
    </sec:authorize>

</div>

在我的标题中

<sec:authorize access="isAuthenticated()">${labelWelcome}
    <sec:authentication property="principal.username" />

用户名不存在(

似乎春天安全不存在(

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="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.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">


    <http use-expressions="true">
        <intercept-url pattern='/WEB-INF' access='permitAll' />
        <form-login login-page="/contacts" authentication-failure-url="/security/loginfail"
                    default-target-url="/contacts" />
        <logout logout-success-url="/contacts"/>
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="user" password="user" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

但是,如果我在浏览器中使用F12,我可以正确看到弹簧返回表单数据

j_username:user
j_password:user
submit:Login
请帮帮我(我的错误在哪里?

UPD: 的web.xml

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


    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>jdbc,mysql</param-value>
        <!--<param-value>hibernate,mysql</param-value>-->
        <!--<param-value>datajpa,mysql</param-value>-->
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <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>
</web-app>

根context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath:spring/app-context.xml"/>
    <import resource="classpath:spring/security-context.xml"/>
</beans>

1 个答案:

答案 0 :(得分:0)

我没有添加pom.xml taglib依赖