【问题标题】:Spring boot security is not loading Angular 7 app build filesSpring Boot 安全性未加载 Angular 7 应用程序构建文件
【发布时间】:2020-04-05 15:50:33
【问题描述】:

我正在尝试通过包含角度构建文件来构建战争文件。我已经在里面放置了 Angular 7 构建文件 src/main/resources/static 文件夹。我使用 Gradle 作为构建工具。

安全类如下所述。

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableGlobalAuthentication
public class JWTWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtUnAuthorizedResponseAuthenticationEntryPoint jwtUnAuthorizedResponseAuthenticationEntryPoint;

    @Autowired
    private UserDetailsService jwtInMemoryUserDetailsService;

    @Autowired
    private JwtTokenAuthorizationOncePerRequestFilter jwtAuthenticationTokenFilter;

    @Value("${jwt.get.token.uri}")
    private String authenticationPath;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .userDetailsService(jwtInMemoryUserDetailsService)
            .passwordEncoder(passwordEncoderBean());
    }

    @Bean
    public PasswordEncoder passwordEncoderBean() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
            .csrf().disable()
            .exceptionHandling().authenticationEntryPoint(jwtUnAuthorizedResponseAuthenticationEntryPoint).and()
            //.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS,"/fbts/api**").permitAll()
            .anyRequest().authenticated().and()
            .httpBasic()
            .and().formLogin().disable();

       httpSecurity
            .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        httpSecurity
            .headers()
            .frameOptions().sameOrigin()  //H2 Console Needs this setting
            .cacheControl(); //disable caching
    }

    @Override
    public void configure(WebSecurity webSecurity) throws Exception {
        webSecurity
            .ignoring()
            .antMatchers(
                HttpMethod.POST,
                authenticationPath
            )
            .antMatchers(HttpMethod.OPTIONS, "/**")
            .and()
            .ignoring()
            .antMatchers(
                HttpMethod.GET,
                "/" //Other Stuff You want to Ignore
            )
            .and()
            .ignoring()
            .antMatchers("/h2-console/**/**");
    }
}

我在尝试访问应用程序时遇到以下问题

我是否要添加任何内容以忽略 .js 文件。我也尝试忽略 .js 文件,但是当我忽略 .js 文件时,我的 Rest API 不安全。任何帮助表示赞赏。

【问题讨论】:

    标签: angular spring spring-boot gradle war


    【解决方案1】:

    在您的最后一行代码.antMatchers("/h2-console/**/**"); 中,您必须添加所有应从授权中排除的资源。 比如

    .antMatchers("/h2-console/**/**"."/resources/**", "/static/**", "/assets/**", "/index.html", "/**/*.css", "/**/*.js", "/**/*.png", "/**/*.jpg", "/**/*.gif", "/**/*.svg", "/**/favicon.ico");

    【讨论】:

    • 感谢您的回复。在加载该应用程序后,我尝试了相同的操作,但是在构建 .war 文件后,Rest API 不受保护。
    • 当我在嵌入式 tomcat 中运行应用程序时,Rest API 是安全的。但是当我将 war 文件部署到外部 tomcat 时,Rest API 是不安全的。
    猜你喜欢
    • 1970-01-01
    • 2017-01-13
    • 2021-11-19
    • 1970-01-01
    • 2019-07-01
    • 2018-10-31
    • 2021-07-24
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多