Java Spring 引导框架上的未知行为
Unknown behaviour on Java Spring Boot Framework
谁能给我解释一下,为什么 Spring Boot 在访问指定的上下文路径时向我发送登录表单作为默认 http 响应:
# configuring Tomcat Server Host
server.address = 127.0.0.1
server.tomcat.threads.max = 20
server.servlet.context-path=/webapp
我通过键入以下内容访问此站点:http://127.0.0.1:8080/webapp
我的文件结构如下所示:
|-.idea
|-.mvn
|-src
|-java
|-resources
|-webapp
|-resources
|-script
|-styles
|-index.html
我预计,默认情况下,在定义上下文路径时,index.html-文件将作为响应发回。相反,我收到了一个不是我自己设计的登录表单。
|-.idea
|-.mvn
|-src
|-java
|-resources
|-static
|-resources
|-script
|-styles
|-index.html
如果您没有启用安全性,您可以使用静态文件夹发布索引html。
如果启用了安全层,则必须对其进行配置。
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@AllArgsConstructor
public class CustomWebSecurity extends WebSecurityConfigurerAdapter {
private final UserService userService;
private final ObjectMapper objectMapper;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().cors().and().authorizeRequests()
.antMatchers(HttpMethod.POST, ChallengeConstant.AUTHORIZE_ENDPOINT).permitAll()
.antMatchers(HttpMethod.POST, ChallengeConstant.TOKEN_ENDPOINT).permitAll()
.antMatchers(HttpMethod.GET, "/*").permitAll()
.antMatchers(HttpMethod.GET, "/assets/**").permitAll()
.anyRequest().authenticated()
.and()
.addFilterBefore(new JWTFilter(userService, objectMapper), UsernamePasswordAuthenticationFilter.class)
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
这里是一个配置示例。
以下两行将跳过索引 html 和资产文件夹的身份验证流程。
.antMatchers(HttpMethod.GET, "/*").permitAll()
.antMatchers(HttpMethod.GET, "/assets/**").permitAll()
Spring 引导安全库自动添加登录表单,并在控制台中为用户名 - 'user' 提供密码。这是 spring 安全性的默认行为。
如果您想配置您需要的用户名和密码,
- 创建配置器 class,它将扩展
WebSecurityConfigurerAdapter
的 Spring 安全性。然后:
- 覆盖它的
configure
方法并在内存中或从数据库中提供您的用户名和密码。
https://howtodoinjava.com/spring-security/
谁能给我解释一下,为什么 Spring Boot 在访问指定的上下文路径时向我发送登录表单作为默认 http 响应:
# configuring Tomcat Server Host
server.address = 127.0.0.1
server.tomcat.threads.max = 20
server.servlet.context-path=/webapp
我通过键入以下内容访问此站点:http://127.0.0.1:8080/webapp
我的文件结构如下所示:
|-.idea
|-.mvn
|-src
|-java
|-resources
|-webapp
|-resources
|-script
|-styles
|-index.html
我预计,默认情况下,在定义上下文路径时,index.html-文件将作为响应发回。相反,我收到了一个不是我自己设计的登录表单。
|-.idea
|-.mvn
|-src
|-java
|-resources
|-static
|-resources
|-script
|-styles
|-index.html
如果您没有启用安全性,您可以使用静态文件夹发布索引html。 如果启用了安全层,则必须对其进行配置。
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@AllArgsConstructor
public class CustomWebSecurity extends WebSecurityConfigurerAdapter {
private final UserService userService;
private final ObjectMapper objectMapper;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().cors().and().authorizeRequests()
.antMatchers(HttpMethod.POST, ChallengeConstant.AUTHORIZE_ENDPOINT).permitAll()
.antMatchers(HttpMethod.POST, ChallengeConstant.TOKEN_ENDPOINT).permitAll()
.antMatchers(HttpMethod.GET, "/*").permitAll()
.antMatchers(HttpMethod.GET, "/assets/**").permitAll()
.anyRequest().authenticated()
.and()
.addFilterBefore(new JWTFilter(userService, objectMapper), UsernamePasswordAuthenticationFilter.class)
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
这里是一个配置示例。
以下两行将跳过索引 html 和资产文件夹的身份验证流程。
.antMatchers(HttpMethod.GET, "/*").permitAll()
.antMatchers(HttpMethod.GET, "/assets/**").permitAll()
Spring 引导安全库自动添加登录表单,并在控制台中为用户名 - 'user' 提供密码。这是 spring 安全性的默认行为。 如果您想配置您需要的用户名和密码,
- 创建配置器 class,它将扩展
WebSecurityConfigurerAdapter
的 Spring 安全性。然后: - 覆盖它的
configure
方法并在内存中或从数据库中提供您的用户名和密码。 https://howtodoinjava.com/spring-security/