Spring Security sec:授权抛出异常

时间:2015-12-09 13:57:01

标签: spring jsp spring-mvc spring-security jsp-tags

我想在jsp中添加一个字段,只显示给admin。为此我使用tag sec:authorize access =“hasRole('Admin')”。但是当我添加它时,应用程序抛出异常:http://pastebin.com/TcN0k0K0
我使用spring 4.1.7.RELEASE,spring-security version 4.0.3.RELEASE。在pom.xml中我添加了spring-security-taglibs v.4.0.3
这是我的jsp代码:

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<html>
<body>    
<sec:authorize access="hasRole('Admin')">
    <p>Must have ROLE_Admin to see this</p>
</sec:authorize>    
<form name='registerForm' method='POST' action="/admin/createuser">
        ...
数据库角色中的

存储为ROLE_Admin,ROLE_User
弹簧security.xml文件

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

     http://www.springframework.org/schema/security/spring-security-4.0.xsd
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

<http use-expressions="true" >
    <csrf disabled="true"/>
    <intercept-url pattern="/admin" access="hasAnyRole('Admin', 'User')" />
    <intercept-url pattern="/" access="permitAll" />
    <intercept-url pattern="/login" access="permitAll" />
    <intercept-url pattern="/logout" access="permitAll" />

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

    <form-login login-page='/login' login-processing-url="/login" authentication-failure-url="/403"
                default-target-url="/admin"
                username-parameter="login" password-parameter="password" />
    <logout logout-url="/logout" logout-success-url="/logoutSuccessful" delete-cookies="JSESSIONID" invalidate-session="true" />
</http>

<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="myDataSource"
                           users-by-username-query= "select login, password, 'true' from employee where login=?"
                           authorities-by-username-query= "select login, role from employee where login =?  " />
    </authentication-provider>
</authentication-manager>

<beans:import resource="data-source-cfg.xml"/>
</beans:beans>

如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

看看这个post,你得到的问题是弹簧版本。您有两种选择:

1 - 要继续使用spring security 4.0.3,必须升级Spring版本4.2.x。

2 - 要继续使用当前的spring版本,必须降级到Spring security 4.0.2

最好的问候