【问题标题】:Spring Security and PAMSpring Security 和 PAM
【发布时间】:2013-11-19 09:18:04
【问题描述】:

我与Spring 3.1.1 合作。 我有 PAM 身份验证,我想使用 spring security 来拒绝用户访问文件。

正常配置是

  1.  <http auto-config="true">
        <intercept-url pattern="/test*" access="ROLE_USER" />
     </http>

  2.  <authentication-manager>
      <authentication-provider>
        <user-service>
        <user name="test" password="test" authorities="ROLE_USER" />
        </user-service>
      </authentication-provider>
    </authentication-manager>

我不想使用第二部分,因为我有 MAP。我想用户第一部分。

我可以这样做吗? 有人可以帮帮我吗?

【问题讨论】:

  • 我不确定你在问什么,但也许这会有所帮助github.com/kohsuke/libpam4j
  • 你如何为你的用户分配角色???您需要定义一个自定义身份验证提供程序(例如使用 UserDetailService)通过 PAM 为用户设置角色)

标签: java spring security authentication pam


【解决方案1】:

您需要定义一个自定义身份验证提供程序,以通过检查 PAM 为用户设置角色。例如,以下代码定义了一个 unix PAM 身份验证:

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String username = String.valueOf(authentication.getPrincipal());
        String password = String.valueOf(authentication.getCredentials());
        PAM pam;
        try {
            pam = new PAM(pamServiceName);
        } catch (PAMException e) {
            throw new AuthenticationServiceException("Could not initialize PAM.", e);
        }
        try {
            UnixUser user = pam.authenticate(username, password);
            return new UsernamePasswordAuthenticationToken(user.getUserName(), authentication.getCredentials(), ROLE_USER);
        } catch (PAMException e) {
            throw new BadCredentialsException("PAM authentication failed.", e);
        } finally {
            pam.dispose();
        }
    }
    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(
          UsernamePasswordAuthenticationToken.class);
    }
}

spring 安全配置:

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

更多示例可以从:https://www.javatips.net/api/org.springframework.security.authentication.badcredentialsexception

【讨论】:

    猜你喜欢
    • 2015-06-11
    • 2019-07-16
    • 2015-06-09
    • 1970-01-01
    • 2016-03-20
    • 2015-12-10
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多