【问题标题】:How to solve 401 - Unauthorised in springboot 2 web app如何解决 401 - 在 springboot 2 web 应用程序中未经授权
【发布时间】:2019-01-05 23:48:54
【问题描述】:

我的控制器:

    @CrossOrigin(origins="http://localhost:3000") 
        @RequestMapping(value="", method = RequestMethod.GET)
        public ResponseEntity<List<Client>> getAllClients(/*@RequestParam("page") int page, @RequestParam("size") int size*/) {

            List<Client> clientList = services.getClientsList(/*page,size*/);
            if(clientList != null) {
                return new ResponseEntity<>(clientList, HttpStatus.OK);
            }else{
                return  new ResponseEntity<>(HttpStatus.BAD_REQUEST);
            }
        }

我的 application.java :展示了我如何尝试使用 bean CorsFilter,我也尝试使用 @CrossOrigin,但我仍然无法战胜这个邪恶的 401,我明白为什么春天会说 @CrossOrigin 是直截了当的甚至不工作,虚假信息弹簧框架正在给人们怎么回事,因为说真的,@CrossOrigin 只是假设可以立即工作,这个安全功能很糟糕 maaan!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

@SpringBootApplication
@EnableJpaRepositories("com.mycompany.Product.repositories")
public class ProductApplication {

@Bean
public FilterRegistrationBean processCorsFilter(){

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();

config.setAllowCredentials(true);
config.addAllowedOrigin("*");             
config.addAllowedHeader("*");
config.addAllowedMethod("*");

source.registerCorsConfiguration("/**", config);                      
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(0);

return bean;

}

/*
    @Bean
    CorsConfigurationSource corsConfiguarationSource(){

        CorsConfiguration configurations = new CorsConfiguration();

        configurations.setAllowedOrigins(Arrays.asList("*"));
        configurations.setAllowCredentials(true);
        configurations.setAllowedHeaders(Arrays.asList("Allow-Control-Allow-Headers","Allow-Control-Allow-Origin","Access-Control-Request-Method","Allow-Control-Request-Headers","Origin","Cache-Origin","Content-Type", "Authorization"));
        configurations.setAllowedMethods(Arrays.asList("DELETE","GET","POST","PATCH","PUT"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configurations);
        return  source;
    }
    /*
    @Bean
    public WebMvcConfigurer corsConfigurer(){

        return new WebMvcConfigurerAdapter(){        
            public void addCorsMappings(CorsRegistry registry){
                registry.addMapping("/client").allowedOrigins("http://localhost:3000");
            }
        };
    }*/

应用程序属性文件:

server.port = 8009
spring.datasource.url = jdbc:mysql://localhost:3306/javaumsdb?useSSL=false
spring.datasource.username = root
spring.datasource.password = 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


spring.jpa.properties.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto = none
spring.jpa.show-sql=true
spring.datasource.name=javaumsdb
spring.datasource.validationQuery = SELECT 1
spring.datasource.whileIdle = true
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy


security.basic.enabled =false
management.security.enabled = false

POM 文件,也许我的 spring 版本与 @CrossOrigin 不兼容:我真的不明白为什么这个错误不会消失......

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-rest-service-cors</artifactId>
    <version>0.1.0</version>

    <name>Product</name>
    <description> project for Spring Boot</description>
        <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
                                <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                    <version>5.0.8.RELEASE</version>
                </dependency>

                 <dependency>
                       <groupId>com.transactioncompany</groupId>
                       <artifactId>cors-filter</artifactId>
                       <version>2.1.2</version>
                </dependency>
                <dependency>
                       <groupId>com.transactioncompany</groupId>
                       <artifactId>java-property-utils</artifactId>
                       <version>1.9.1</version>
                </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-neo4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
                <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-entitymanager</artifactId>
                        <version>5.2.3.Final</version>
                </dependency>
                <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-core</artifactId>
                        <version>5.2.17.Final</version>
                </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
                <dependency>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus</artifactId>
                        <version>3.3.1</version>
                </dependency>
                <!-- https://mvnrepository.com/artifact/org.codehaus.plexus/plexus-utils -->
                <dependency>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus-utils</artifactId>
                        <version>3.1.0</version>
                </dependency>

                <!-- https://mvnrepository.com/artifact/org.codehaus.mojo/exec-maven-plugin -->
                <dependency>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.6.0</version>
                </dependency>

               <!-- <dependency>
                    <artifactId>gwt-maven-plugin</artifactId>
                    <groupId>org.codehaus.mojo</groupId>
                    <version>1.3.1</version>
                </dependency> -->
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
                        <plugin>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>exec-maven-plugin</artifactId>
                        </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 401 错误与 CORS 完全无关。这只是意味着您未通过身份验证,并试图访问需要您身份验证的资源。
  • 401 表示未经授权,请检查您的请求标头或身份验证机制。
  • 使用用户名 = 用户和密码 = spring 日志生成的随机字符串登录后,我已被重定向到 localhost:8080/login ...我只看到客户端信息,但我的端口上没有任何反应3000/angular ...我如何验证或绕过此问题...提前感谢您的帮助。
  • 在邮递员中我得到 401,在浏览器中我得到 302...;浏览器说我已被重定向并且不允许访问 localhost:8009/client
  • 我迷路了@Emre 你所说的身份验证机制是什么意思?我是否应该在我的数据库中使用其中一个用户进行登录?

标签: java maven spring-boot cors


【解决方案1】:

您是否提供了 WebMvcConfigurer 的实现。请创建此配置类并尝试。

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
   registry.addMapping("/**").allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", 
"PATCH");
  }
}

【讨论】:

  • 我以前做过,如果你检查我的 application.Java 我已经尝试过了,但它没有工作......但是在邮递员中,我能够使用带有基本身份验证的 get 请求获得 200OK。这是否意味着我必须为从 angular 到 java 的每个请求发送基本身份验证,
  • 我已经添加了 btao 标头。现在我仍然收到 401 但不同的消息:无法加载localhost:8009/client:预检响应具有无效的 HTTP 状态代码 401... 谁能解释这部分,做我需要忽略后端的选项或?我能闻到我们已经非常接近解决方案了
猜你喜欢
  • 2022-10-15
  • 2020-01-24
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多