Java Spring安全自定义登录函数

Java Spring安全自定义登录函数,java,spring,spring-security,vaadin,Java,Spring,Spring Security,Vaadin,我正在开发Vaadin/Spring应用程序。对于登录,建议使用Spring安全性。在文档[1]之后,我设置了spring security。现在我正在应用程序中使用带有硬编码用户名/密码的InMemoryUserDetailsManager @Bean @Override public UserDetailsService userDetailsService() { UserDetails user = User.withUsername("user

我正在开发Vaadin/Spring应用程序。对于登录,建议使用Spring安全性。在文档[1]之后,我设置了spring security。现在我正在应用程序中使用带有硬编码用户名/密码的
InMemoryUserDetailsManager

@Bean
@Override
public UserDetailsService userDetailsService() {
    UserDetails user =
            User.withUsername("user")
                    .password("{noop}pass")
                    .roles("USER")
                    .build();

    return new InMemoryUserDetailsManager(user);
}
一切都与此设置一起工作,但是对于日志记录,我调用了一个外部函数,该函数返回一个带有所提供用户名/密码对的布尔值,并且非内置管理器允许这样做

canLogin(user,pass);
这进而调用外部服务。如何设置spring security以允许此操作

[1]

贝尔东的文章有一个我认为适合您需要的例子

只需创建自己的身份验证提供程序,根据需要进行身份验证,然后将其注册到安全配置中

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private MyAuthenticationService myAuthenticationService;

    @Override
    public Authentication authenticate(Authentication authentication) 
      throws AuthenticationException {
 
        String name = authentication.getName();
        String password = authentication.getCredentials().toString();
        
        if (myAuthenticationService.canLogin(name, password)) {
            return new UsernamePasswordAuthenticationToken(
              name, password, new ArrayList<>());
        } else {
            return null;
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}
@组件
公共类CustomAuthenticationProvider实现AuthenticationProvider{
@自动连线
私有MyAuthenticationService MyAuthenticationService;
@凌驾
公共身份验证(身份验证)
抛出AuthenticationException{
String name=authentication.getName();
字符串密码=authentication.getCredentials().toString();
if(myAuthenticationService.canLogin(名称、密码)){
返回新的用户名PasswordAuthenticationToken(
名称、密码、新ArrayList());
}否则{
返回null;
}
}
@凌驾
公共布尔支持(类身份验证){
返回authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
贝尔东的文章有一个我认为适合您需要的例子

只需创建自己的身份验证提供程序,根据需要进行身份验证,然后将其注册到安全配置中

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private MyAuthenticationService myAuthenticationService;

    @Override
    public Authentication authenticate(Authentication authentication) 
      throws AuthenticationException {
 
        String name = authentication.getName();
        String password = authentication.getCredentials().toString();
        
        if (myAuthenticationService.canLogin(name, password)) {
            return new UsernamePasswordAuthenticationToken(
              name, password, new ArrayList<>());
        } else {
            return null;
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}
@组件
公共类CustomAuthenticationProvider实现AuthenticationProvider{
@自动连线
私有MyAuthenticationService MyAuthenticationService;
@凌驾
公共身份验证(身份验证)
抛出AuthenticationException{
String name=authentication.getName();
字符串密码=authentication.getCredentials().toString();
if(myAuthenticationService.canLogin(名称、密码)){
返回新的用户名PasswordAuthenticationToken(
名称、密码、新ArrayList());
}否则{
返回null;
}
}
@凌驾
公共布尔支持(类身份验证){
返回authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}

所以您只需要得到一个布尔值。没有用户信息?@SimonMartinelli仅仅是一个表示成功或失败的布尔值,所以你只需要得到一个布尔值。没有用户信息?@SimonMartinelli仅仅是一个表明成功或失败的宝莱恩