spring-security-rest - 如何在使用 Spring Security 的 Spring Boot 应用程序中跳过对 HAL 浏览器的授权
问题描述
我想让 HAL 浏览器跳过对 Spring Boot 应用程序的授权。我正在使用 Spring Security 进行授权。
这是 build.gradle 文件中条目的快照
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-security'
我的 Spring Boot 应用程序在端口 2128 上运行
http://localhost:2128/browser/index.html会在 Spring Security 实现之前打开 HAL 浏览器。我尝试在下面给出的 SecurityConfiguration 类的配置方法中添加 .antMatchers("/browser/index.html ").permitAll()**。我还尝试覆盖public void configure(WebSecurity web)方法来忽略 URL
背景:在我实施 Spring 安全授权之前,HAL 浏览器正在工作。实施弹簧安全后,它停止工作。
@配置 @EnableWebSecurity 公共类 SecurityConfiguration 扩展 WebSecurityConfigurerAdapter { @覆盖 受保护的无效配置(AuthenticationManagerBuilder auth)抛出异常{ auth.authenticationProvider(daoAuthenticationProvider()); } @覆盖 受保护的无效配置(HttpSecurity http)抛出异常{ http .csrf().disable() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) 。和() .addFilter(new AuthorizationFilter(authenticationManager(), userRepository)) .authorizeRequests() // 配置访问规则 .antMatchers("/browser/index.html**").permitAll() .anyRequest().authenticated(); http.headers().frameOptions().disable(); } @覆盖 公共无效配置(WebSecurity web)抛出异常{ web.ignoring().antMatchers("/browser/index.html"); } }
公共类 AuthorizationFilter 扩展 BasicAuthenticationFilter { 公共静态最终字符串 HEADER_STRING_REMOTE_USER = "远程用户"; /** * 安全管道由不同的过滤器组成,因此我们需要委托给管道的其余部分。 * * @param 请求 * @param 响应 * @param 链 * @throws IOException * @throws ServletException */ @覆盖 protected void doFilterInternal (HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { // 读取 Authorization 标头,我们在其中获取 userId String userId = request.getHeader(HEADER_STRING_REMOTE_USER); // 如果 header 不包含 userId 或者是 null 委托给 Spring impl 并退出 if (userId == null) { 链.doFilter(请求,响应); 返回; } // 如果 userId 存在,尝试从数据库中获取用户主体并执行授权 身份验证身份验证 = getUsernamePasswordAuthentication(userId); SecurityContextHolder.getContext().setAuthentication(authentication); // 继续过滤器执行 链.doFilter(请求,响应); } 私人身份验证 getUsernamePasswordAuthentication (String userId) { // 如果我们通过 userId 找到用户,则在 DB 中搜索 // 如果是这样,则获取用户详细信息并使用用户名、密码、权限/角色创建 spring 身份验证令牌 if (userId != null) { 列出用户 = userRepository.findByUserId(userId); UserPrincipal 主体 = 新 UserPrincipal(user.get(0)); UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(principal, null, principal.getAuthorities()); 返回授权; } 返回空值; } }
有没有人遇到过类似的问题...
解决方案
我最终做的是使用弹簧活动配置文件进行管理。
有关弹簧型材的更多信息,请参阅链接
我为“安全”配置文件启用了 Spring Security,并为“开发”配置文件禁用了它。因此,在“开发”配置文件中,HAL 浏览器可以在没有任何安全中断的情况下工作。
@Configuration
@EnableWebSecurity
@Profile("secure")
public class WebSecurityConfigEnable extends WebSecurityConfigurerAdapter {
@Autowired
UserPrincipalDetailsService userPrincipalDetailsService;
private UserRepository userRepository;
@Value("${spring.profiles.active}")
private String activeProfile;
public WebSecurityConfigEnable (UserPrincipalDetailsService
userPrincipalDetailsService, UserRepository userRepository) {
this.userPrincipalDetailsService = userPrincipalDetailsService;
this.userRepository = userRepository;
}
@Override
protected void configure (AuthenticationManagerBuilder auth) throws
Exception {
auth.authenticationProvider(daoAuthenticationProvider());
}
@Override
protected void configure (HttpSecurity http) throws Exception {
http
.cors().configurationSource(request -> new
CorsConfiguration().applyPermitDefaultValues())
.and()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilter(new AuthorizationFilter(authenticationManager(),
userRepository, activeProfile))
.authorizeRequests()
// configure access rules
.anyRequest().authenticated();
}
@Bean
DaoAuthenticationProvider daoAuthenticationProvider () {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setPasswordEncoder(this.passwordEncoder());
daoAuthenticationProvider.setUserDetailsService(this.userPrincipalDetailsService);
return daoAuthenticationProvider;
}
@Bean
PasswordEncoder passwordEncoder () {
return new BCryptPasswordEncoder();
}
}
用于在“dev”配置文件中运行应用程序
java -jar -Dspring.profiles.active=dev build\libs\springApp-0.1.1-SNAPSHOT.jar
推荐阅读
- r - 如何从 R raster 包中格式化 plotRGB 中的轴?
- python - 叶地图未显示在 Django 网页中
- php - WordPress:动态更改类别
- python - 根据单独系列的索引对 Pandas DataFrame 的列执行替换的最快方法是什么?
- azure-devops - Azure DevOps 是否支持每个项目的多个模板?
- java - 用外部依赖测试类的最佳方法是什么
- python - Django 安装和环境不再被识别
- android - 在 ViewHolder 中实现单击侦听器并访问关联项
- python - 制作嵌套字典的单独实例的更好解决方案?
- linux - ARM 交叉编译器找不到包含的标准库