我目前正在研究基于java的发票应用程序。 我需要的是,授权用户与多个角色,例如彼得应该有角色ROLE_SETTING,ROLE_REPORT,ROLE_QUOTE,而安妮只有一个角色ROLE_SETTING等...春季安全jdbc配置授权用户与多个角色
这是我的spring-security-config.xml
<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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<http auto-config="true">
<access-denied-handler error-page="/403page" />
<intercept-url pattern="/makebill*" access="ROLE_ADMIN,ROLE_MAKEBILL" />
<intercept-url pattern="/report*" access="ROLE_ADMIN,ROLE_REPORT" />
<intercept-url pattern="/stock*" access="ROLE_ADMIN,ROLE_STOCK" />
<intercept-url pattern="/customer*" access="ROLE_ADMIN,ROLE_CUSTOMER" />
<!-- <intercept-url pattern="/setting*" access="ROLE_ADMIN,ROLE_SETTING" /> -->
<intercept-url pattern="/mainmenu*" access="ROLE_ADMIN,ROLE_MAKEBILL,ROLE_SETTING,ROLE_CUSTOMER,ROLE_REPORT,ROLE_STOCK" />
<form-login
login-page='/login' username-parameter="username"
password-parameter="password" default-target-url="/mainmenu"
authentication-failure-url="/login?authfailed" />
<logout logout-success-url="/login?logout" />
</http>
<authentication-manager>
<authentication-provider>
<jdbc-user-service
data-source-ref="dataSource"
users-by-username-query="select username,password, enabled from person where username=?"
authorities-by-username-query="select username, role from person, role where username =? " />
</authentication-provider>
</authentication-manager>
</beans:beans>
认证管理器目前工作的罚款只有一个角色,我的意思是......当SQL查询authorities-by-username-query="select username, role from person, role where username =? "
运行,如果返回值只是一个角色,如ROLE_REPORT
,应用程序工作正常,但如果在db中有一个记录,如ROLE_REPORT,ROLE_SETTING,ROLE_CUSTOMER
,当查询检索到这个值时,应用程序将返回我404错误,看来我将无法授予用户m多个角色。
任何人都可以请指出我做错了什么。 谢谢。
有几个地方,你需要解决。首先,通过用户名查询的权限是不正确的。我认为你是SQL查询的新手。您正在获取2个表格而没有使用任何键将它们连接起来。请看看这个 http://stackoverflow.com/questions/20455139/users-by-username-query-returns-no-results。这可能无法解决您的所有问题。让我知道你是否有另一个不同的问题。 – Tin
你可能会发现这个教程很有用:http://www.mkyong.com/spring-security/spring-security-form-login-using-database/ – Tin
mkyong的啧啧是为一个用户授予一个角色,我需要的是多个角色为单个用户... – seph