即使没有安全性,SpringBoot 401 UnAuthorized

新手上路,请多包涵

我没有使用 Spring Security,但它要求我进行身份验证。

在此处输入图像描述

URL 异常( http://localhost:8080/SpringJob/ExecuteJob ):

 {
    "timestamp": 1500622875056,
    "status": 401,
    "error": "Unauthorized",
    "message": "Bad credentials",
    "path": "/SPPA/ExecuteSPPAJob"
}
----below log details
2017-07-21 13:15:35.210  INFO 19828 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/SpringJob]   : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-07-21 13:15:35.210 [http-nio-8080-exec-1] INFO
                    o.a.c.c.C.[.[localhost].[/SpringJob]-Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-07-21 13:15:35.211  INFO 19828 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-07-21 13:15:35.211 [http-nio-8080-exec-1] INFO
                    o.s.web.servlet.DispatcherServlet-FrameworkServlet 'dispatcherServlet': initialization started
2017-07-21 13:15:35.494  INFO 19828 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 282 ms
2017-07-21 13:15:35.494 [http-nio-8080-exec-1] INFO
                    o.s.web.servlet.DispatcherServlet-FrameworkServlet 'dispatcherServlet': initialization completed in 282 ms

应用程序-dev.xml

 #Spring Boot based configurations
management.security.enabled: "false"
spring.autoconfigure.exclude:  "org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration"
spring.batch.job.enabled: false
server.contextPath: /SpringJob

build.gradle 片段

plugins {
    id 'jacoco'
    id 'org.sonarqube' version '2.5'
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: "no.nils.wsdl2java"
apply plugin: 'jacoco'
apply plugin: "org.sonarqube"
dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-batch")
    compile("org.springframework.boot:spring-boot-starter-mail")
    //compile("org.springframework.boot:spring-boot-devtools")
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.5'
    compile group: 'org.apache.cxf', name: 'cxf-spring-boot-starter-jaxws', version: '3.1.10'
    compile group: 'org.apache.cxf', name: 'cxf-rt-ws-security', version: '3.1.10'
    compile("org.springframework.boot:spring-boot-starter-actuator")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

控制器

@Controller
@EnableAutoConfiguration
@EnableBatchProcessing
public class MyController {
    @Autowired
    JobLauncher jobLauncher;

    @RequestMapping("/ExecuteJob")
    @ResponseBody
    public String callPrelegalJob(@RequestParam("user") String userName, @RequestParam("password") String password) {
        log.info("Job is to be launched from controller...");
}
}

原文由 sunleo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

尝试在您的 application.properties 文件中添加以下行

security.basic.enable: false
security.ignored=/**

根据 spring doc,使用 security.ignored=

要从默认安全路径中排除的以逗号分隔的路径列表

原文由 Afridi 发布,翻译遵循 CC BY-SA 3.0 许可协议

在当前版本的 Spring Boot (v2.1.0.RELEASE) 中,摆脱安全问题的最简单方法是将“WebSecurityConfig.java”添加到您的项目中,如下所示:

 import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

}

当然请注意,这取消了对跨站点请求伪造的保护,因此这实际上只适用于简单的只读端点。

原文由 trevorsky 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题