相关疑难解决方法(0)

添加了 corsConfigurationSource,但仍然出现错误“已被 CORS 策略阻止”

我正在尝试将 Spring Security 连接到我的项目。\n创建了 Security Config 类

\n
@Configuration\n@EnableWebSecurity\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\n\n    private final JwtTokenProvider jwtTokenProvider;\n\n    @Bean\n    @Override\n    public AuthenticationManager authenticationManagerBean() throws Exception {\n        return super.authenticationManagerBean();\n    }\n    \n    public SecurityConfig(JwtTokenProvider jwtTokenProvider) {\n        this.jwtTokenProvider = jwtTokenProvider;\n    }\n\n    @Override\n    protected void configure(HttpSecurity http) throws Exception {\n        http\n                .httpBasic().disable()\n                .cors().and().csrf().disable()\n                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)\n                .and()\n                .authorizeRequests()\n                .antMatchers("/auth/api/v1/user/register").permitAll()\n                .antMatchers("/auth/api/v1/admin/*").hasRole("ADMIN")\n                .anyRequest().authenticated()\n                .and()\n                .apply(new JwtConfigurer(jwtTokenProvider));\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我正在从浏览器发送注册请求

\n
http://localhost:15001/auth/api/v1/user/register\n
Run Code Online (Sandbox Code Playgroud)\n

我得到了答案:

\n
Access to XMLHttpRequest at \'http://localhost:15001/auth/api/v1/user/register\' from origin \'http://localhost:4200\' has been blocked by CORS policy: …
Run Code Online (Sandbox Code Playgroud)

spring-security

5
推荐指数
1
解决办法
2740
查看次数

为SPA前端配置Spring Boot

我有整个前端部分在资源中铺设的应用程序.我想将事情分开.并且具有用于UI的单独服务器,例如由gulp提供.

因此,我假设我的服务器应该返回index.html客户端呈现的所有请求.

例如:我有'user /:id'路由,它通过角度路由进行管理,不需要任何服务器.如何配置以便服务器不会重新加载或重定向到任何地方?

我的安全配置正在跟随(不知道它是否负责此类事情):

public class Application extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**", "/app/**", "/app.js")
                .permitAll().anyRequest().authenticated().and().exceptionHandling()
                .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout()
                .logoutSuccessUrl("/").permitAll().and().csrf()
                .csrfTokenRepository(csrfTokenRepository()).and()
                .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
                .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
    } 
Run Code Online (Sandbox Code Playgroud)

java spring angularjs single-page-application gulp

3
推荐指数
2
解决办法
5353
查看次数