spring-security 使用vaadin保护 Spring Boot

mum43rcc  于 2022-11-11  发布在  Spring
关注(0)|答案(1)|浏览(211)

我尝试使用BD用户进行身份验证,而不是使用InMemory Auth,因此创建了类用户和角色,并将此代码添加到该类中

@EnableWebSecurity
@Configuration
public class SecurityConfig extends VaadinWebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserDetails customUserDetails;

    @Autowired
    CustomAuthenticationProvider customAuthenticationProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // Set default security policy that permits Vaadin internal requests and
        // denies all other
        super.configure(http);
        setLoginView(http, LoginView.class, "/logout");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //auth.inMemoryAuthentication().withUser("user").password("{noop}userpass").roles("USER");
        auth.userDetailsService(customUserDetails).passwordEncoder(passwordEncoder());
    }

    @Bean
    public DaoAuthenticationProvider createDaoAuthenticationProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setUserDetailsService(customUserDetails);
        provider.setPasswordEncoder(passwordEncoder());
        return provider;
    }
}

但当我尝试验证时,它不接受

8yoxcaq7

8yoxcaq71#

有不止一种方法可以做到这一点,但通常大致是这样的。相关的部分,完整的解决方案是太长的SO。不应该有任何Vaadin的具体,但只是遵循Spring文档的问题。

@EnableWebSecurity
@Configuration
public class SecurityConfig extends VaadinWebSecurityConfigurerAdapter {
    ...
    private final UserDetailsService userDetailsService;

    @Autowired
    public SecurityConfig (UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        super.configure(auth);
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
    }
    ...
}

然后根据Spring规范实现UserDetailsService

@Service
@Primary
public class UserDetailsServiceImpl implements UserDetailsService {
    ...
    @Autowired
    private UserRepository repository; // or what ever you have named it ...

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // implement this according to your actual implementation of user database
    }

}

相关问题