使用Spring Security从静态文件夹服务Angular 2项目

Bat*_*azi 2 spring spring-mvc spring-security angular

因此,我在Angular2上有一个工作的前端,在Java上有一个工作的后端,我要做的是从静态文件夹中提供index.html,该文件夹还包含我所有的前端资源。问题是,当我尝试将Spring Security添加到后端时,由于@EnableWebSecurity批注,资源不再可访问。当我导航到本地主机http:// localhost:8080 /时,index.html不被提供。但是,如果我访问它或任何其他手动编写路径的资源,它将加载。我不想为前端服务有所不同,有什么办法可以从静态方式做到这一点?我尝试了以下方法:

这里是我的安全配置:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ComponentScan(basePackages = {"com.ramso.restapi.security"})
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

public static final String REMEMBER_ME_KEY = "rememberme_key";

public SecurityConfig() {
    super();
    logger.info("loading SecurityConfig ................................................ ");
}

@Autowired
private UserDetailsService userDetailsService;

@Autowired
private RestUnauthorizedEntryPoint restAuthenticationEntryPoint;


@Autowired
private AuthenticationSuccessHandler restAuthenticationSuccessHandler;

@Autowired
private AuthenticationFailureHandler restAuthenticationFailureHandler;

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


@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/front/**","/index.html");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .headers().disable()
        .csrf().disable()
        .authorizeRequests()
            .antMatchers("/failure").permitAll()
            .anyRequest().authenticated()
            .and()
        .exceptionHandling()
            .authenticationEntryPoint(restAuthenticationEntryPoint)
            .and()
        .formLogin()
            .loginPage("/login")
            .loginProcessingUrl("/authenticate")
            .successHandler(restAuthenticationSuccessHandler)
            .failureHandler(restAuthenticationFailureHandler)
            .usernameParameter("username")
            .passwordParameter("password")
            .permitAll()
            .and()
        .logout()
            .logoutUrl("/logout")
            .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
            .deleteCookies("JSESSIONID")
            .permitAll()
            .and();

}
}
Run Code Online (Sandbox Code Playgroud)

WebMvcConfiguration:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
//registry.addViewController("/").setViewName("front/index.html");
//registry.addViewController("/").setViewName("forward:/index.html");
    registry.addViewController("/").setViewName("redirect:/index.html");

registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}

}
Run Code Online (Sandbox Code Playgroud)

Application.java:

@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)

Tom*_*Tom 5

在延伸的课程中 WebSecurityConfigurerAdapter可以添加以下内容:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/front/**");
}
Run Code Online (Sandbox Code Playgroud)

web.ignoring()Spring Security应该忽略您放入方法中的任何蚂蚁匹配器。

默认情况下,静态内容应放置在以下目录之一src/main/resources(在spring boot-static content下):

/META-INF/resources/
/resources/
/static/
/public/
Run Code Online (Sandbox Code Playgroud)

然后在子文件夹的前面检查任何蚂蚁匹配器。

例如,如果您的静态内容在src/main/resources/static/frontant匹配器中/front/**则应忽略该子文件夹中的所有资源。

另外,为了公开index.html您应该将其放入src/main/resources/static并添加类似以下类的内容,以便在访问您的网站时将其公开为主要资源:

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

}
Run Code Online (Sandbox Code Playgroud)

并将其添加为蚂蚁匹配器: web.ignoring().antMatchers("/*", "/front/**", "index.html");

/*不会允许所有的,/**这样做。确保将API放置在诸如此类的安全端点上/api,并将静态内容置于忽略的路径上。