#java #spring #security
Вопрос:
Я написал код для статьи https://spring.io/guides/gs/securing-web/ Но когда я захожу на страницу входа в систему, я получаю ошибку 404. Мой исходный код ничем не отличается от кода из статьи. В чем заключается ошибка кода?
MvcConfig.java исходный код
package config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
}
WebSecurityConfig.java исходный код
package config;
import org.springframework.context.annotation.Bean;
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;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>flowers</groupId>
<artifactId>fowers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>fowers</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
В чем заключается ошибка кода?
Ответ №1:
registry.addViewController("/login").setViewName("login");
Я думаю, проблема в этом инсульте. Вы пытаетесь переписать отображение страницы /входа в систему и добавить логин для просмотра, но я не вижу этого в вашем проекте.
В чем дело login.html досье?
Комментарии:
1. login.html файл существует <!DOCTYPE html> <!DOCTYPE html><html > <html ><head> <head><title>Пример безопасности Spring <title></title> </title></head> </head><body> <body><действие формы=»/логин» метод=»сообщение»> <действие формы=»/логин» метод=»сообщение»><div><div><метка> Имя пользователя : <метка><тип ввода=»текст» имя=»имя пользователя»/> <тип ввода=»текст» имя=»имя пользователя»/></метка></метка></div> <div><div><метка>Пароль:<метка> <тип ввода=»пароль» имя=»пароль» /><тип ввода=»пароль» имя=»пароль»/> </метка></метка></div></div> <div><div><тип ввода=»отправить» значение=»Войти»/><тип ввода=»отправить» значение=»Войти»/></div></div> </форма></форма> </тело></тело> </html>
Ответ №2:
Вы положили свой login.html
файл в resources/templates/
папку. Я скопировал в точности ваш исходный код, и он работает гладко без ошибки 404.