【问题标题】:Spring MVC, Spring Security, Simply Require UsernameSpring MVC,Spring Security,只需要用户名
【发布时间】:2011-11-12 23:46:24
【问题描述】:

我有一个使用 Spring Security 3.1.0RC2 的 Spring 3 MVC 网站。目前我正在使用org.springframework.security.ldap.authentication.ad.ActiveDirectoryAuthenticationProvider 进行登录。出于演示目的,我的老板只想输入一个用户名(任何用户名),而不是对其进行任何验证,而是授予访问权限。有什么方法可以确保用户输入了用户名,仅此而已?

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    为什么不直接编写自己的 AuthenticationProvider。

    class MyAuthProvider implements AuthenticationProvider {
        public Authentication authenticate(Authentication authentication) {
            // add code to populate GrantedAuthority list if needed.
            Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            authorities.add(new GrantedAuthorityImpl("ROLE_1"));
            authorities.add(new GrantedAuthorityImpl("ROLE_2"));
            authorities.add(new GrantedAuthorityImpl("ROLE_3"));
            return new UsernamePasswordAuthenticationToken(
                authentication.getPrincipal(), 
                authentication.getCredentials(), 
                authorities);
    
        }
    
        public boolean supports(Class<?> authentication) {
            return true;
        }
    }
    

    在您的 spring 安全配置中:

    <security:authentication-manager>
        <security:authentication-provider ref="myAuthenticationProvider" />
    </security:authentication-manager>
    <bean id="myAuthenticationProvider" class="MyAuthProvider"/>
    

    更新 1 有关如何向用户添加权限(授予角色)的信息,请参见上文。

    【讨论】:

    • 完美,谢谢。最后一个问题,如何为用户添加角色?
    猜你喜欢
    • 2014-01-17
    • 2019-01-03
    • 2018-01-26
    • 2012-11-21
    • 2021-10-27
    • 2018-04-15
    • 2012-10-08
    • 2020-07-26
    • 2014-08-12
    相关资源
    最近更新 更多