我正在学习 Spring boot,虽然我可以运行我的项目,但当我尝试访问它时,希望看到一个空白页面,我收到登录/密码提示,尽管我在应用程序中定义了一些值.properties 文件,我总是收到错误。
pom.xml
是:
<?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>b.c.g</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>appName</name>
<description>appDescription</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</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-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</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-mobile</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-social-facebook</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-social-twitter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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-web-services</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
是:
spring.datasource.url = jdbc:mysql://localhost:3306/app
spring.datasource.name=app
spring.datasource.username=app
spring.datasource.password=appPassword
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
security.user.name=master
security.user.password=abc123
最后,我的主要类(class)是:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@Controller
public class AppNameApplication {
@ResponseBody
@RequestMapping(value = "/")
public String entry() {
return "AppName";
}
public static void main(String[] args) {
SpringApplication.run(AppNameApplication.class, args);
}
}
虽然我是一名经验丰富的 Spring 用户,但我正在学习 Spring boot 以提高我的开发技能。
我在浏览器上收到的错误是:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Aug 09 13:33:59 BRT 2016 There was an unexpected error (type=Unauthorized, status=401). Bad credentials
我知道现在没有发生什么事情,但这是否是预期的行为?我至少应该能够登录吗?
最佳答案
我会将此答案作为更详细的“评论”发布,因为我无法提供详细的评论。
在您的 pom.xml 中,您已添加 Spring Security 启动器:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
这意味着 Spring 将启用安全性来访问您的端点,因此会出现未经授权错误。 Spring Security 的默认行为是锁定整个应用程序。
因此,您必须相应地配置 spring security 才能启用您的访问权限。
详见此链接https://spring.io/guides/gs/securing-web/
您必须创建一个 WebSecurity 配置器,如下示例:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
在使用 Spring Security 之前,您可以首先删除安全启动器并尝试访问您的端点。一旦您确认您的 API 按预期工作,您就可以开始使用安全性了。同样,我建议你从头开始,没有入门者,在学习的同时将它们一一添加,而不是一次性添加所有内容。
关于java - 如何登录我的新 Spring Boot 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38856488/