我正在学习Spring启动,当我可以运行我的项目时,当我尝试访问它时,希望有一个空白页面,我会收到登录/密码提示,虽然我已经定义了一些我的application.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
最后,我的主要课程是:
@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启动以增加我的开发技能。
我在浏览器上遇到的错误是:
Whitelabel错误页面
这个应用程序没有/ error的显式映射,所以你看到了 这是一个后备。 Tue Aug 09 13:33:59 BRT 2016有一个 意外错误(type = Unauthorized,status = 401)。凭证错误
我知道现在没有什么事情发生,但这是预期的行为吗?难道我至少不能登录吗?
答案 0 :(得分:2)
我会将此答案更详细地发布为评论&#34;,因为我无法对其进行评论。
在你的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按预期工作后,即可开始使用安全性。同样地,我建议你从零开始,没有任何启动器并逐个添加,同时你学习而不是在一次拍摄中添加所有内容。