首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >弹簧安全UserDetailsService不工作

弹簧安全UserDetailsService不工作
EN

Stack Overflow用户
提问于 2016-08-26 12:57:55
回答 4查看 7.9K关注 0票数 2

好了,伙计们,希望你们能帮我,这是我最后一次尝试。我对这个春天的安全世界很陌生,我不能让它起作用。我尝试了很多东西,学习了很多教程,什么也没做。

问题是,正如您在标题中所看到的,要使自定义用户详细信息服务正常工作。它只是没有登录,它似乎没有调用定制用户详细服务,因为sysouts没有显示在控制台中.

它作为一个魅力与春天安全的记忆功能。下面是我的密码。

弹簧安全控制:

代码语言:javascript
运行
复制
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = CustomUserDetailsService.class)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
UserDetailsService userDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    //auth.inMemoryAuthentication().withUser("ram").password("ram123").roles("ADMIN");
    auth.userDetailsService(userDetailsService).passwordEncoder(passwordencoder());
}

@Override
public void configure(WebSecurity web) throws Exception {
  web
    .ignoring()
       .antMatchers("/bower_components/**", "/resources/**", "/img/**"); // #3
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().and().csrf().disable()
    .authorizeRequests()
      .antMatchers("/", "/home", "/call").permitAll() // #4
      .antMatchers("/resource", "/video").hasRole("USER") // #6
      .anyRequest().authenticated();
}

@Bean(name="passwordEncoder")
public PasswordEncoder passwordencoder(){
    return new BCryptPasswordEncoder();
}

}

CustomUserDetailsService

代码语言:javascript
运行
复制
@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService{

private UserService userService;

@Override
public UserDetails loadUserByUsername(String ssoId)
        throws UsernameNotFoundException {
    User user = userService.findByUsername(ssoId);
    System.out.println("User : "+user);
    if(user==null){
        System.out.println("User not found");
        throw new UsernameNotFoundException("Username not found");
    }
        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), 
             user.isEnabled(), true, true, true, getGrantedAuthorities(user));
}


private List<GrantedAuthority> getGrantedAuthorities(User user){
    List<GrantedAuthority> authorities = new ArrayList<>();

    authorities.add(new SimpleGrantedAuthority(user.getRole()));

    System.out.print("authorities :"+authorities);
    return authorities;
}
}

初始化类

代码语言:javascript
运行
复制
@SpringBootApplication
@EnableWebSocket
public class One2OneCallApp implements WebSocketConfigurer {

@Bean
public CallHandler callHandler() {
  return new CallHandler();
}

@Bean
public UserRegistry registry() {
  return new UserRegistry();
}

@Bean
public KurentoClient kurentoClient() {
  return KurentoClient.create();
}

@Bean
public UiApplication uiApplication(){
  return new UiApplication();
}

@Bean
public CustomUserDetailsService customUserDetailsService(){
  return new CustomUserDetailsService();
}

@Bean
public SecurityConfig securityConfig(){
  return new SecurityConfig();
}

@Bean
public EncryptPassword encryptPassword(){
  return new EncryptPassword();
}

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry   registry) {
  registry.addHandler(callHandler(), "/call");
}

public static void main(String[] args) throws Exception {
  System.out.println("Iniciando");  
  new SpringApplication(One2OneCallApp.class).run(args);    
}

}

我还测试了与数据库的通信,它运行得非常好。我在寻求任何帮助。抱歉英语不太好。谢谢大家!

编辑:下面回答了我自己的问题。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-09-14 12:44:59

最后,为了我不得不做的演示,我一直呆在内置的记忆花絮中。我认为我的问题与spring启动和应用程序中的初始化有关。

票数 -6
EN

Stack Overflow用户

发布于 2016-08-26 13:12:07

SecurityConfig类中:

代码语言:javascript
运行
复制
@Autowired
CustomUserDetailsService customUserDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserDetailsService);
}
票数 0
EN

Stack Overflow用户

发布于 2016-08-26 13:44:16

变化

代码语言:javascript
运行
复制
@Autowired
UserDetailsService userDetailsService;

代码语言:javascript
运行
复制
@Autowired
CustomUserDetailsService userDetailsService;

另外,在web/套接字配置中导入安全配置,并将组件扫描移到那里,而不是在安全性上。

代码语言:javascript
运行
复制
@ComponentScan(basePackageClasses = CustomUserDetailsService.class)
@Import(value = { SecurityConfig.class })
public class WebConfig extends WebMvcConfigurerAdapter { /*...*/ }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39166920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档