【问题标题】:Custom Authentication Provider is not getting called Spring Security自定义身份验证提供程序没有被称为 Spring Security
【发布时间】:2015-03-02 12:20:31
【问题描述】:

我正在尝试实现一个自定义 AuthenticationProvider 来验证对我所有受保护 URL 的调用。我已经实现了所有方法并相信我的 xml 配置是正确的,但是对受保护 URL 的调用总是转到 entry-point-ref 类(我在其中抛出 401 错误)。这是跳过我的身份验证提供程序(我的身份验证方法中有日志语句并且知道它没有被调用)。从其他帖子中我了解到 support 方法可能是罪魁祸首,但我已将其设置为始终返回 true,所以这不应该是我的问题。

我的安全上下文代码:

    <?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:context="http://www.springframework.org/schema/context"
    xmlns:security="http://www.springframework.org/schema/security"
    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
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <security:http 
            use-expressions="true"
            auto-config="false"
            create-session="stateless"
            entry-point-ref="unauthorizedEntryPoint"
            authentication-manager-ref="authenticationManager">
        <security:intercept-url pattern="/User/sign_up/*" access="permitAll" />
        <security:intercept-url pattern="/User/authenticate/**" access="permitAll" />
        <security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    </security:http>

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

    <beans:bean id="customAuthenticationProvider" class="pathto:CustomAuthenticationProvider" />

    <beans:bean id="unauthorizedEntryPoint" class="pathto:UnauthorizedEntryPoint" /

web.xml 的代码:

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

    The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/security-context.xml
        </param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <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>

未授权入口点:

public class UnauthorizedEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException authenticationException) throws IOException, ServletException {
        System.out.println("in unauth");
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication Failure: UnauthorizedEntryPoint Token invalid");
    }

}

AuthenticationProvider:(目前这里不做任何认证)

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication auth)
            throws AuthenticationException {
        System.out.println("in authenticate");
        String username = auth.getName();
        String password = auth.getCredentials().toString();
        List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
        System.out.println("username: "+username+". password: "+ password);
        if (true) { //checkPassword(password, user.getPasswords())) {
            AUTHORITIES.add(new SimpleGrantedAuthority("ROLE_USER"));
            return new UsernamePasswordAuthenticationToken(auth.getName(), auth.getCredentials(), AUTHORITIES);
        }
        return null;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return true;
    }

更新: 我正在使用参数 j_username 和 j_password 使用 Advanced Rest Client 对此进行测试。这是正确的还是问题所在?

您对可能出现的问题有任何见解吗?

谢谢!

【问题讨论】:

  • 我最终采用了不同的方式,并设置了一个自定义过滤器来调用它,类似于 here 所做的那样。
  • 我面临着类似的问题。你能提供一些细节你是如何解决的吗?您提供的链接已失效。

标签: java authentication spring-security


【解决方案1】:

您没有配置 Web 身份验证机制,因此无需调用身份验证管理器。结果,请求总是会到达您的身份验证入口点(为未经身份验证的用户调用),并且您会看到观察到的错误。通常,身份验证入口点会执行重定向到登录表单或发送WWW-Authenticate 标头等操作。

您需要配置表单或基本身份验证等内容(请参阅手册或项目随附的示例)。请注意,您还将应用程序配置为无状态,因此每个请求都需要进行身份验证。

【讨论】:

  • 谢谢,是的,这就是我对自定义过滤器所做的。在我实现 REST 服务时需要无状态部分。
【解决方案2】:

您为什么不删除 security:http 下的 authentication-manager-ref ?你已经声明了authentication-manager 标签。

<security:http 
        use-expressions="true"
        auto-config="false"
        create-session="stateless"
        entry-point-ref="unauthorizedEntryPoint">
    <security:intercept-url pattern="/User/sign_up/**" access="permitAll" />
    <security:intercept-url pattern="/User/authenticate/**" access="permitAll" />
    <security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
</security:http>

此外;不是您的问题的一部分,而只是建议:

@Override
public boolean supports(Class<?> authentication) {
    return authentication.equals(UsernamePasswordAuthenticationToken.class);
}

【讨论】:

  • 我删除了 authentication-manager-ref 并再次尝试(它仍然无法正常工作)。我刚刚让 support 方法返回 true 的原因是尝试调用它。在我做到这一点之后,我会更紧地施放它。我是否通过传入 j_username 和 j_password 正确测试它?
  • 作为一种变通方法,您可以使用 &lt;user-service&gt; 而不是自定义应用程序提供程序来测试您的配置。
  • 这并不是我真正想要实现的,因为我不想为 UserDetails 实现提供所有信息。我需要实现过滤器吗?我只是不明白为什么它不会被当前配置调用
  • 我尝试实施此解决方法,但是,我仍然总是被发送到 UnauthorizedAccessPoint。这让我相信这是我如何配置它的方式。我不应该使用入口点引用吗?
猜你喜欢
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 2016-07-19
  • 2021-12-25
  • 2016-09-24
  • 2011-01-03
  • 2016-01-06
  • 2014-05-01
相关资源
最近更新 更多