【发布时间】:2021-06-01 08:23:20
【问题描述】:
有一个项目在 localhost:8080 上运行 Spring boot 后端,在 localhost:4200(用户网站)和 localhost:4201(管理网站)上运行 2 个前端 Angular 应用程序。我该如何配置 spring 安全性它只允许用户网站中具有角色 - ROLE_USER,ROLE_ADMIN 的用户和角色 -ROLE_ADMIN 的用户可以访问管理网站。
目前两个用户都可以访问两个 wesbite。有没有办法限制某些域而不是限制用户的路径(URL)。
当前配置 -
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
Environment env;
@Autowired
UserSecurityService useSecurityService;
private BCryptPasswordEncoder passwordEncoder() {
return SecurityUtility.passwordEncoder();
}
private static final String[] PUBLIC_MATHCES= {
"/css/**",
"/js/**",
"/images/**",
"/book/**",
"/user/**",
"/media/**"
};
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(useSecurityService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(PUBLIC_MATHCES).permitAll()
.anyRequest().authenticated()
.and();
http.csrf().disable()
.cors()
.and()
.httpBasic();
}
@Bean
public HttpSessionIdResolver httpSessionStrategy() {
return HeaderHttpSessionIdResolver.xAuthToken();
}
}
【问题讨论】: