【问题标题】:spring security does not authenticatespring security 不进行身份验证
【发布时间】:2014-02-01 04:12:37
【问题描述】:

我正在使用 spring security 3.2.0 和 spring framework 4.x 编写一个简单的 web 应用程序。我可以看到用户名/密码在自定义身份验证提供程序中通过,但实际身份验证尚未完成;也就是说,当输入错误的密码时,我仍然会被带到登录后的 url /dashboard...

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring-config.xml
                /WEB-INF/spring-security-config.xml
            </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>frontController</servlet-name>
        <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
  </servlet>
    <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>

    <servlet-mapping>
        <servlet-name>frontController</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    </web-app>

spring-security-config.xml

<?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-4.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security.xsd">  
    <http>
        <intercept-url pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <intercept-url pattern="/dashboard*" access="ROLE_USER" />

        <form-login login-page='/login' default-target-url='/dashboard'
            always-use-default-target='true' authentication-failure-url="/login?error=true" />
        <logout logout-success-url="/" />

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

    </authentication-manager>

</beans:beans>

自定义身份验证提供程序除了委派给用户服务之外不做任何事情:

 public class AuthenticationProvider extends DaoAuthenticationProvider {
     //nothing here
 }

用户服务通过id获取用户详细信息并注入到上面的auth provider中:

public class UserLoginService implements UserDetailsService{

    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
    UserDetail ud = new UserDetail();
    User u = new User();
    ud.setUser(u);
    u.setUsername("reza");
    u.setPassword("reza");
    u.setAccountNonExpired(true);
    u.setAccountNonLocked(true);
    u.setCredentialsNonExpired(true);
    u.setEnabled(true);
    u.setRole("ROLE_USER");
    return ud;
    }


public class UserDetail implements org.springframework.security.core.userdetails.UserDetails {

    User user;
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(1);
         authList.add(new SimpleGrantedAuthority(user.getRole()));
        return authList;
    }
    @Override
    public String getPassword() {

        return user.getPassword();
    }
    @Override
    public String getUsername() {
        return user.getUsername();
    }
    @Override
    public boolean isAccountNonExpired() {
        return user.isAccountNonExpired();
    }
    @Override
    public boolean isAccountNonLocked() {
        return user.isAccountNonLocked();
    }
    @Override
    public boolean isCredentialsNonExpired() {
        return user.isCredentialsNonExpired();
    }
    @Override
    public boolean isEnabled() {
        return user.isEnabled();
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
}

【问题讨论】:

  • 您确实需要为您的身份验证提供程序和 UserDetailsS​​ervice 添加代码。
  • 发布您的完整 UserLoginService
  • 我在 auth 提供者上重写了 authenticate 方法并调用了 super.authenticate() 并且它起作用了。但对我来说,这一步似乎没有必要......

标签: java spring authentication spring-mvc spring-security


【解决方案1】:

我重写了身份验证提供程序上的身份验证方法并调用了 super.authenticate() 并且它起作用了。

【讨论】:

    猜你喜欢
    • 2016-09-13
    • 2012-11-27
    • 2019-02-07
    • 2013-12-16
    • 2011-10-17
    • 1970-01-01
    • 2011-04-30
    • 2012-03-06
    相关资源
    最近更新 更多