Spring Boot 基本身份验证在循环中询问用户名和密码

Spring Boot basic authentication asking username and password in loop

提问人:Siddu 提问时间:2/4/2023 更新时间:2/4/2023 访问量:116

问:

我正在尝试使用 mysql 和 JPA 实现 spring boot 安全性。实施安全性后,每当我尝试使用swagger击中任何api时,它都会不断要求输入用户名和密码,即使在多次输入正确的用户名和密码后,它也会弹出相同的弹出窗口。

下面是 java 类和错误屏幕截图

'''

 public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


@Autowired
UserDetailsService userDetailsService;

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

@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity.authorizeHttpRequests()
                .antMatchers("/student/updateStudent").hasRole("USER")
                .antMatchers("/swagger-ui").permitAll()
                .and()
                .httpBasic();

    httpSecurity.csrf().disable();
}

public PasswordEncoder getPasswordEncoder() {
    return NoOpPasswordEncoder.getInstance();
}

}

@Service
public class CustomUserDetailService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    // TODO Auto-generated method stub
    return new CustomUserDetails(username);
}

}

public class CustomUserDetails implements UserDetails {

private String userName;

public CustomUserDetails(String userName) {
    super();
    this.userName = userName;
}

public CustomUserDetails() {
    super();
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    // TODO Auto-generated method stub
    return Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
}

@Override
public String getPassword() {
    // TODO Auto-generated method stub
    return "password";
}

@Override
public String getUsername() {
    // TODO Auto-generated method stub
    return userName;
}

@Override
public boolean isAccountNonExpired() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isAccountNonLocked() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isCredentialsNonExpired() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isEnabled() {
    // TODO Auto-generated method stub
    return true;
}

}

'''

enter image description here

MySQL Spring Security JPA 启动

评论


答: 暂无答案