成功登录后,Spring MVC-Security Access被拒绝

时间:2017-05-03 12:19:38

标签: spring spring-mvc spring-security

您好我有春季MVC webb应用程序并使用jdbc-user-service作为身份验证管理器。我已经配置了所有内容,并且在成功登录后我被重定向到../home(因为我应该)并且它应该写我的名字但是它写了“访问被拒绝”

Spring xml文件 `

<http auto-config="true">
    <intercept-url pattern="/home" access="hasRole('ROLE_USER, ROLE_ADMIN')"/>
    <intercept-url pattern="/home/**" access="hasRole('ROLE_USER, ROLE_ADMIN')"/>
    <intercept-url pattern="/" access="permitAll"/>
    <intercept-url pattern="/login" access="permitAll"/>


    <form-login
        login-processing-url="/j_spring_security_check"
        login-page="/login"
        authentication-success-handler-ref="authenticationSucessHandler"
        authentication-failure-url="/login/error"
        username-parameter="userName"
        password-parameter="userPassword"
        always-use-default-target="true"/>
    <logout
    invalidate-session="true"
    delete-cookies="JSESSIONID"/>
    <csrf />
    <headers>
        <frame-options policy="SAMEORIGIN"/>
    </headers>
</http>

<beans:bean name="authenticationSucessHandler" class="sk.icz.log.viewer.security.AuthenticationSuccessHnadler"/>

<authentication-manager>
    <authentication-provider>

        <jdbc-user-service
                data-source-ref="dataSource"
                users-by-username-query="select username, pass, enable from PUBLIC.users where username=?"
                authorities-by-username-query="select username, rol from PUBLIC.user_roles where username=?"
        />
    </authentication-provider>
</authentication-manager>

<jdbc:embedded-database id="dataSource" type="HSQL">
    <jdbc:script location="classpath:db/schemaCreate.sql"/>
    <jdbc:script location="classpath:db/addUser.sql"/>
</jdbc:embedded-database>

<beans:bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    <beans:constructor-arg ref="dbcpDataSource"/>
</beans:bean>

<beans:bean id="dbcpDataSource" class="org.apache.commons.dbcp2.BasicDataSource"
      destroy-method="close">
    <beans:property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <beans:property name="url" value="jdbc:hsqldb:mem:dataSource" />
    <beans:property name="username" value="sa" />
    <beans:property name="password" value="" />
</beans:bean>

<beans:bean depends-on="dataSource" class="org.springframework.beans.factory.config.MethodInvokingBean">
    <beans:property name="targetClass" value="org.hsqldb.util.DatabaseManagerSwing"/>
    <beans:property name="targetMethod" value="main"/>
    <beans:property name="arguments">
        <beans:list>
            <beans:value>--url</beans:value>
            <beans:value>jdbc:hsqldb:mem:SKUSKA</beans:value>
            <beans:value>--user</beans:value>
            <beans:value>sa</beans:value>
            <beans:value>--password</beans:value>
            <beans:value></beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

`

schema_create.sql

create table users(
  username varchar(20),
  pass varchar(20),
  enable int
);
create table user_roles(
  username varchar(20),
  rol varchar(20)
);

addUser.sql

insert into users values('admin', '123', 1);

insert into user_roles values('admin', 'ROLE_USER');

我没有用PFKeys创建关系数据库,以防我想尝试这个(我知道数据库构建错误)

1 个答案:

答案 0 :(得分:1)

修改

  

hasAnyRole(角色列表) - 如果已授予用户任何权限,则为true   指定的角色(以逗号分隔的字符串列表给出)。

当使用paper-input spring期望单个角色时,在您的情况下,您可能希望使用hasRole并提供多个角色。最重要的是你错过了报价。修改这两行并查看它是否解决了您的问题:

更改自:

hasAnyRole

要:

 <intercept-url pattern="/home" access="hasRole('ROLE_USER, ROLE_ADMIN')"/>
 <intercept-url pattern="/home/**" access="hasRole('ROLE_USER, ROLE_ADMIN')"/>