【问题标题】:Why does it require authentication to load DB values?为什么加载数据库值需要身份验证?
【发布时间】:2019-01-05 05:23:56
【问题描述】:

在我的 spring-boot + reactjs 应用程序中,我使用 spring security 进行身份验证和角色管理。

应用程序运行良好,但需要成功的身份验证才能加载下拉列表的值,这些下拉列表是动态的,它们是从数据库表中加载的。实际上,我并没有为查询数据库添加任何安全限制。

谁能帮我找出问题所在?

这是我的git url

请以'src/main/java/com/example/polls/controller/FamilyController.java'为例

我尝试将以下代码放入 SecurityConfig.java 但它不起作用

http.authorizeRequests() 
    .antMatchers("/brands").permitAll()      
    .antMatchers("/familys").permitAll()
    .antMatchers("/models").permitAll(); 

在控制台显示

ERROR 9640 --- [io-8080-exec-10] c.e.p.s.JwtAuthenticationEntryPoint      : Responding with unauthorized error. Message - Full authentication is required to access this resource
ERROR 9640 --- [nio-8080-exec-1] c.e.p.s.JwtAuthenticationEntryPoint      : Responding with unauthorized error. Message - Full authentication is required to access this resource

修改代码:

         http
        .cors()
            .and()
        .csrf()
            .disable()
        .exceptionHandling()
            .authenticationEntryPoint(unauthorizedHandler)
            .and()
        .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
        .authorizeRequests()
            .antMatchers(
                "/",
                "/favicon.ico",
                "/**/*.png",
                "/**/*.gif",
                "/**/*.svg",
                "/**/*.jpg",
                "/**/*.html",
                "/**/*.css",
                "/**/*.js"
                ) .permitAll()
            .antMatchers("/api/auth/**")
                .permitAll()
            .antMatchers("/api/user/checkUsernameAvailability", "/api/user/checkEmailAvailability")
                .permitAll()
            .antMatchers(HttpMethod.GET, "/api/polls/**", "/api/users/**")
                .permitAll()
            .antMatchers("/api/brands").permitAll()      
            .antMatchers("/api/familys").permitAll()
            .antMatchers("/api/models").permitAll()
            .anyRequest()
                .authenticated();

   http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

【问题讨论】:

    标签: java spring reactjs spring-boot spring-security


    【解决方案1】:

    你的控制器被"/api/familys"映射

    ....
    @RestController
    @RequestMapping("/api/familys")
    public class FamilyController { 
    ....
    

    所以你的安全模式匹配器应该首先以/api/ 开始

    并且还要确保在.anyRequest().authenticated();之前声明这个端点的顺序在spring sec中很重要

      .antMatchers(HttpMethod.GET, "/api/polls/**", "/api/users/**").permitAll()
      .antMatchers("/api/brands/**").permitAll()      
      .antMatchers("/api/familys/**").permitAll()
      .antMatchers("/api/models/**").permitAll()
    .anyRequest()
            .authenticated();; 
    

    【讨论】:

      猜你喜欢
      • 2013-12-24
      • 2018-02-04
      • 2011-09-29
      • 2018-03-05
      • 1970-01-01
      • 2013-03-26
      • 2012-06-08
      • 2018-12-28
      • 2021-05-17
      相关资源
      最近更新 更多