当前位置: 首页 > 知识库问答 >
问题:

带有Spring Security性和Java配置的自定义身份验证管理器

轩辕鸿
2023-03-14

我使用Spring Security和SpringMVC来创建一个与现有应用程序(我将把它称为BackendApp)对话的web应用程序(为了清晰起见,我将把它称为WebApp)。

我希望将身份验证责任委托给BackendApp(这样我就不需要同步两个应用程序)。

我知道我需要编写一个自定义的身份验证管理器来实现这一点,但是我对spring非常陌生,找不到关于如何实现它的任何信息。

我相信我需要做这样的事情:

public class CustomAuthenticationManager implements AuthenticationManager{

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

        String username = authentication.getName();
        String pw       = authentication.getCredentials().toString();

        // Code to make rest call here and check for OK or Unauthorised.
        // What do I return?

    }

}

如果成功,我是否设置authentication.setauthenticated(true);如果不成功,我是否设置false?

编写完成后,如何配置spring security以使用java配置文件使用此身份验证管理器?

提前感谢您的帮助。

共有1个答案

公孙锋
2023-03-14

看看下面我的样本。必须返回UsernamePasswordAuthenticationToken。它包含委托人和授权人。希望我能帮上忙:)

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = authentication.getPrincipal() + "";
    String password = authentication.getCredentials() + "";

    User user = userRepo.findOne(username);
    if (user == null) {
        throw new BadCredentialsException("1000");
    }
    if (!encoder.matches(password, user.getPassword())) {
        throw new BadCredentialsException("1000");
    }
    if (user.isDisabled()) {
        throw new DisabledException("1001");
    }
    List<Right> userRights = rightRepo.getUserRights(username);
    return new UsernamePasswordAuthenticationToken(username, null, userRights.stream().map(x -> new SimpleGrantedAuthority(x.getName())).collect(Collectors.toList()));
}

PS:userRepo和rightRepo是Spring-Data-JPA存储库,它们访问我的自定义User-DB

SpringSecurity JavaConfig:

@Configuration
@EnableWebMvcSecurity
public class MySecurityConfiguration extends WebSecurityConfigurerAdapter {

public MySecurityConfiguration() {
    super(false);
}

@Override
protected AuthenticationManager authenticationManager() throws Exception {
    return new ProviderManager(Arrays.asList((AuthenticationProvider) new AuthProvider()));
}

}
 类似资料:
  • 问题内容: 这是我的情况: 一个Web应用程序对许多应用程序执行某种SSO 登录的用户,而不是单击链接,该应用就会向正确的应用发布包含用户信息(名称,pwd [无用],角色)的帖子 我正在其中一个应用程序上实现SpringSecurity以从其功能中受益(会话中的权限,其类提供的方法等) 因此,我需要开发一个 自定义过滤器 -我猜想-能够从请求中检索用户信息,通过自定义 DetailsUserSe

  • 如何通过使用Spring Security和Java配置来定义自定义身份验证提供程序?我想在我自己的数据库上执行一个登录检查凭据。

  • 我有Springmvc应用程序。我添加了带有CustomAuthentiationManager的Spring Security。这是appC中包含的application-security.xmlontex.xml.login.jsp我使用带有2个输入的标准登录页面:name='j_username'

  • 我正在编写一个程序,它使用了带有Spring Security的JWT身份验证。我已经实现了自定义授权和身份验证过滤器。另外,我需要持久化我的令牌,它是由这些过滤器形成的。为此,我创建了令牌DAO服务,它自动连接到过滤器,并用注释标记我的过滤器以自动连接该服务。但我无法正确自动执行身份验证管理器。 我尝试在安全配置类中公开身份验证管理器bean,但没有结果。 这个错误是我在尝试构建项目时遇到的。

  • 现在我的是的一部分,位于正确的位置。平滑。 但是,由于是,它会被引导拾取并作为筛选器包含。所以我的过滤链看起来真的像: SSOAuthenticationFilter(由于是,因此包含) FilterChainProxy(spring自动配置) ... SecurityContextPersistenceFilter SSOAuthenticationFilter(包含在) ... 显然,我希望在

  • 我的问题是我的SpringConfig类中筛选器的配置。我希望筛选器仅在请求是/authenticate URL时生效,我已将.antmatcher(“/authenticate/**”)添加到筛选器配置中。 当所有其他URL中的该行不再安全时,我可以手动导航到/home,而不进行身份验证,删除该行并对/home进行身份验证。 我应该声明一个只适用于特定URL的筛选器吗? 我如何在维护其他URL的