【问题标题】:Multiple spring security configuration not working多个弹簧安全配置不起作用
【发布时间】:2018-01-14 19:01:24
【问题描述】:

在我的应用程序中,我希望有基于 url 模式的单独的 spring 安全实现。

例如。 /rest/ ** 将拥有自己的身份验证提供程序(基本身份验证)和

/web/ ** 将有自己的身份验证提供程序(表单登录)。

请在下面找到我已经完成的配置

<?xml version="1.0"?>

<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.xsd">

<!-- config for rest services using basic auth-->

<http pattern="/rest/**">
    <intercept-url pattern="/MyAppRestServices" access="permitAll" />
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    <http-basic />
</http>

<!-- AUTHENTICATION MANAGER FOR CUSTOM AUTHENTICATION PROVIDER -->

<authentication-manager alias="authenticationManager">
    <authentication-provider ref="customAuthenticationProvider" />
</authentication-manager>

<!-- config for web using form login--> 

<http pattern="/web/**">
    <intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')" />
    <form-login/>
</http>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="admin" password="nimda" authorities="ROLE_ADMIN" />
        </user-service>
    </authentication-provider>
</authentication-manager>

在上面的配置中,第一个配置工作正常,即带有基本身份验证的 restservice,但带有表单登录配置的 web 不起作用。它甚至没有拦截网址?

请告诉我上面的配置有什么问题?

【问题讨论】:

  • 看来您的第二次拦截正在寻找 /web/**/** 作为您的网址。这是故意的吗?如果没有,您可以从 html 中删除模式“/web/**”并将其添加到拦截 URL。除非你打算添加更多的 url 来拦截。

标签: spring spring-security


【解决方案1】:

请参考以下 web 身份验证的工作配置::

<?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 pattern="/css/**" security="none" />
    <http pattern="/images/**" security="none" />
    <http pattern="/js/**" security="none" />

    <http auto-config="false" authentication-manager-ref="dev" use-expressions="true" disable-url-rewriting="true">
        <intercept-url pattern="/admin/login" access="permitAll" />
        <intercept-url pattern="/admin/*" access="isAuthenticated()" />
        <form-login
            login-page="/admin/login"
            default-target-url="/admin/workbench"
            username-parameter="username"
            password-parameter="password"
            authentication-failure-url="/admin/login"
        />
        <logout logout-success-url="/admin/login" logout-url="/j_spring_security_logout" invalidate-session="true" delete-cookies="JSESSIONID" />
    </http>

    <beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />

    <!-- STATIC USER -->
    <authentication-manager id="dev" alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="abc" password="pwd" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 2011-06-22
    • 2019-04-27
    • 2017-01-12
    • 2015-06-06
    • 2018-10-31
    相关资源
    最近更新 更多