Spring Security应用程序提供了找不到org.springframework.security.authentication.UsernamePasswordAuthenticationToken的身份验证提供程序。

19 浏览
0 Comments

Spring Security应用程序提供了找不到org.springframework.security.authentication.UsernamePasswordAuthenticationToken的身份验证提供程序。

我是初次接触Spring Boot。我正在尝试在Spring Tool Suite(STS)中使用userdetailsservice实现简单的Spring Boot安全性。

下面是我使用的控制器代码:

@RestController

public class HomeController {

@GetMapping("/")

public String home() {

return("

Welcome

");

}

@GetMapping("/user")

public String user() {

return("

Welcome user

");

}

}

以及Web安全配置代码:

@EnableWebSecurity

public class AppSecureConfig extends WebSecurityConfigurerAdapter {

@Autowired

UserDetailsService userDetailsService;

@Autowired

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

auth.userDetailsService(userDetailsService);

}

@Override

protected void configure(HttpSecurity http) throws Exception {

http.authorizeRequests()

.antMatchers("/user").hasRole("USER")

.antMatchers("/").permitAll()

.and().formLogin()

.and().logout().permitAll();

}

@Bean

public PasswordEncoder getPasswordEncoder() {

return NoOpPasswordEncoder.getInstance();

}

}

我在pom.xml中添加了所有必需的依赖项。

所以,我在application.properties文件中添加了以下行,现在系统不再生成安全密码。

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration

并且我在凭据中包含了用户详细信息服务。

以下是用户详细信息服务类:

@Service

public class MyuserDetails implements UserDetailsService {

@Override

public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

return new userPrincipal(s);

}

}

和用户凭据类:

public class userPrincipal implements UserDetails {

private static final long serialVersionUID = 1L;

private String userName;

public userPrincipal(String userName) {

this.userName = userName;

}

public userPrincipal() {

}

@Override

public Collection getAuthorities() {

return Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));

}

@Override

public String getPassword() {

return "pass";

}

@Override

public String getUsername() {

return userName;

}

@Override

public boolean isAccountNonExpired() {

return true;

}

@Override

public boolean isAccountNonLocked() {

return true;

}

@Override

public boolean isCredentialsNonExpired() {

return true;

}

@Override

public boolean isEnabled() {

return true;

}

}

现在,当我使用http://localhost:8081/网址运行应用程序时,它会显示“找不到适用于org.springframework.security.authentication.UsernamePasswordAuthenticationToken的身份验证提供程序”。

我正在使用Spring Tool Suite(STS)运行此项目。有人能指出我在这里做错了什么吗?

0
0 Comments

在使用Spring Security应用程序时,可能会遇到" No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken"的问题。这个问题的出现原因是缺少身份验证提供程序。下面是解决该问题的方法:

1. 在application.properties中添加以下配置:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration

2. 修改类如下:

public class AppSecureConfig extends WebSecurityConfigurerAdapter {
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("{noop}"+"pass").roles("USER");
    }
    protected void configure(HttpSecurity http) throws Exception {
         http.authorizeRequests()
         .antMatchers("/user").hasRole("USER")
         .antMatchers("/").permitAll()
         .and().formLogin()
         .and().logout().permitAll();
    }
}

通过添加上述配置和修改类,可以解决" No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken"问题。

0
0 Comments

在使用Spring Security应用程序时,出现了"No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken"的问题。这个问题的出现原因是因为在配置中排除了整个SecurityAutoConfiguration,而不是只排除org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration。解决方法有两种:

第一种方法是不要排除整个SecurityAutoConfiguration,而是只排除org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration。

第二种方法是暴露一个UserDetailsService bean,该bean将为您完成相同的工作,并且可以摆脱configureGlobal方法。具体代码如下:

public UserDetailsService userDetailsService() {
    UserDetails user = User.builder()
            .username("user")
            .password("password")
            .roles("USER")
            .build();
    return new InMemoryUserDetailsManager(user);
}

通过以上两种方法,可以解决"No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken"的问题。

0