【问题标题】:Why is my custom authentication not working in Spring Security 3?为什么我的自定义身份验证在 Spring Security 3 中不起作用?
【发布时间】:2017-02-09 12:57:56
【问题描述】:

我在尝试实现自己的自定义身份验证时遇到了 spring security 3 的问题。按照this页面步骤我写了这个类:

public class CustomAuth implements AuthenticationManager {

@Override
public Authentication authenticate(Authentication auth)
        throws AuthenticationException {

    UserService service = new UserService();

    User user = service.login((String) auth.getPrincipal(), new String(
            DigestUtils.sha256((String) auth.getCredentials())));

    LinkedList<GrantedAuthority> authorities = new LinkedList<>();

    if (user != null) {
        authorities.add(new SimpleGrantedAuthority(user.getRole()));

        return new UsernamePasswordAuthenticationToken(user.getUsername(),
                user.getPassword(), authorities);
    }

    return null;
}

}

这是我的 spring-security.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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.1.xsd
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-3.1.xsd">


<security:http pattern="/resources/**" security="none" />

<security:http auto-config="true" >

    <security:intercept-url pattern="/user/**"
        access="ROLE_USER" />
    <security:intercept-url pattern="/admin/**"
        access="ROLE_ADMIN,ROLE_USER" />

    <security:form-login login-page="/login"
        authentication-failure-url="/login?error=true" />

    <security:logout invalidate-session="true" />

    <security:session-management>
        <security:concurrency-control
            max-sessions="1" />

    </security:session-management>


</security:http>
<security:authentication-manager>
    <security:authentication-provider ref="myAuthProvider" />

</security:authentication-manager>


    <bean id="myAuthProvider" class="org.jhonnytunes.security.CustomAuth">

</bean>

</beans>

tomcat7 正在记录 this,而应用程序未在浏览器中显示。

我正在使用:

  1. Eclipse 开普勒
  2. Ubuntu 13.04
  3. JDK 1.7
  4. Tomcat7
  5. Eclipse STS 插件

这可能是什么?

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    CustomAuth 应该实现 AuthenticationProvider,而不是 AuthenticationManager

    【讨论】:

      【解决方案2】:

      实现'AuthenticationProvider'而不是'AuthenticationManager'

      'throw new BadCredentialsException (String)' 而不是 'return null'

      【讨论】:

        猜你喜欢
        • 2014-01-28
        • 2021-04-06
        • 1970-01-01
        • 2017-02-13
        • 2014-04-20
        • 2014-12-13
        • 2014-01-12
        • 2017-02-10
        • 1970-01-01
        相关资源
        最近更新 更多