【发布时间】:2020-04-10 00:40:12
【问题描述】:
我正在使用 SpringBoot 进行一个学校项目,在添加了一些在互联网上看到的安全功能(WebSecurityConfigurerAdapter)之后,我想知道是否有可能提取当前登录用户的用户名,因为我需要他的 id关于控制器的其他方法 为了从数据库中提取一些信息。
我的 SecurityConfig 类看起来像这样
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/*").access("hasRole('USER')")
.antMatchers("/admin/*").hasRole("ADMIN")
.and()
// some more method calls
.formLogin();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("flaviu")
.password("yes").roles("USER");
}
@SuppressWarnings("deprecation")
@Bean
public static NoOpPasswordEncoder passwordEncoder() {
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}
}
【问题讨论】:
标签: java spring-boot spring-security