【发布时间】:2020-06-01 13:55:58
【问题描述】:
我有一个基于 Spring Boot 构建的 REST Web 服务。对于身份验证,我使用 Spring Security 和 Basic Authentication,我对此并不陌生。我的解决方案是否必须使用角色和权限?
我只希望 Web 服务的用户说明用户名和密码凭据。我在数据库或其他地方没有任何用户。
在我的 WebSecurityConfigurerAdapter 配置中,我现在在 configureGlobal 方法的末尾有 .authorities("ROLE_USER");,下面是 Internet 上的示例。我想跳过那个,这样课程看起来像这样:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("username").password(passwordEncoder().encode("password"));
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated().and().httpBasic();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
我发现我做不到。如果我改用.roles("USER"),我会工作。但我既不想处理权限也不想处理角色,只想处理用户名和密码。
为什么我需要这个或如何跳过这个?
我很高兴您能就此事(身份验证和 Spring Security)向新手提供任何启示。
【问题讨论】:
-
Spring Security Reference 是开始阅读该主题的好地方。使用 Spring Boot,您只需要 dependency 而这个 example 将有助于满足您的要求。
标签: rest spring-boot web-services spring-security basic-authentication