Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
spring security 403错误_Spring_Spring Mvc_Spring Security - Fatal编程技术网

spring security 403错误

spring security 403错误,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我正试图按照网上的指南使用Spring security来保护我的网站。因此,在我的服务器端,WebSecurity配置适配器和控制器如下所示 @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter implements ApplicationContextAware { @Override protected void registerAuthe

我正试图按照网上的指南使用Spring security来保护我的网站。因此,在我的服务器端,WebSecurity配置适配器和控制器如下所示

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
implements ApplicationContextAware {

@Override
protected void registerAuthentication(AuthenticationManagerBuilde r authManagerBuilder) throws Exception {
authManagerBuilder.inMemoryAuthentication()
.withUser("user").password("password").roles("ADMI N");
}
}

@Controller
//@RequestMapping("/course")
public class CourseController implements ApplicationContextAware{

@RequestMapping(value="/course", method = RequestMethod.GET, produces="application/json")
public @ResponseBody List<Course> get(// The critirion used to find.
@RequestParam(value="what", required=true) String what,
@RequestParam(value="value", required=true) String value) {
//.....
}

@RequestMapping(value="/course", method = RequestMethod.POST, produces="application/json")
public List<Course> upload(@RequestBody Course[] cs) {
}
}

我在网上搜索了好几天了。还是不知道。请帮忙。非常感谢

问题可能是由于。如果用户不在web浏览器中使用您的应用程序,请提供保护。否则,你应该确保

要执行此操作,您可以使用以下命令:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig
    extends WebSecurityConfigurerAdapter implements ApplicationContextAware {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // ...
            .csrf().disable();
    }

    @Override
    protected void registerAuthentication(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
        authManagerBuilder
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("ADMIN");
    }
}

检查您通过“Header”发送的令牌,并在数据库中查询该令牌是否存在


注意:以上仅适用于使用Spring引导令牌身份验证机制的情况

该问题可能与CSRF或CORS安全保护有关

  • 对于CSRF:如果应用程序用户没有从浏览器中使用它,您可以禁用它
  • 对于CORS:您可以指定来源并允许HTTP方法
下面的代码禁用CSRF并允许所有源和HTTP方法。所以使用它时要注意

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter  implements WebMvcConfigurer {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedMethods("*");
    }

}

我也找了好几天了!只需使用http.CSRF().disable()在configure方法上禁用CSRF;是我停止接收403的put请求所需完成的全部操作。

谢谢Rob。这是csrf问题。是的,我不希望我的用户使用web浏览器来使用我的应用程序。它在客户端运行良好。但是在服务器端,我得到了这样的消息:o.s.web.servlet.PageNotFound:请求方法'POST'不受支持。我应该忽略它吗?我认为PageNotFound是一个单独的问题,我会解决。您可能需要发布另一个问题,其中包含有关此错误的更多信息。是否正确?角色(“管理员”)。“I”和“N”之间有一个空格。在生产中禁用csrf()是否有危险?请不要禁用csrf。这是一个巨大的安全风险。如果您不知道这一点,我强烈建议您在禁用它之前先查找它。不鼓励只使用代码的答案
http.httpBasic().disable();
http.authorizeRequests().antMatchers("/signup").permitAll().antMatchers("/*")
     .fullyAuthenticated().and().formLogin()
     .and().csrf().disable();
http.csrf().disable();
http.httpBasic().disable();
http.authorizeRequests().antMatchers("/signup").permitAll().antMatchers("/*")
     .fullyAuthenticated().and().formLogin()
     .and().csrf().disable();
http.csrf().disable();