Java 如何使用Spring Boot连接身份验证

Java 如何使用Spring Boot连接身份验证,java,spring,spring-boot,Java,Spring,Spring Boot,我遵循从到使用Spring Boot设置用户身份验证的文档 以下是相关文件: MvcConfig.java package com.*********.*******; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springfram

我遵循从到使用Spring Boot设置用户身份验证的文档

以下是相关文件:

MvcConfig.java

package com.*********.*******;

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("/home").setViewName("home");
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");
    }

}
WebSecurityConfig.java

package com.*********.*******;

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("/", "/home").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);
    }
}
如何消除home.html文件并将其连接起来,以便localhost:808将用户直接带到登录,如果成功,将用户带到hello.html文件

如果我将默认控制器更改为以下代码,那么它只会进入登录页面的一个循环,因为登录会将您带到您尝试访问的上一个页面。如何让它将用户发送到hello.html

public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/hello").setViewName("hello");
        registry.addViewController("/login").setViewName("login");
    }
如果这篇文章措词不当,我道歉。如果有什么我可以补充清楚的,请不要犹豫,我会做任何更正。

  • 删除行
    .antMatchers(“/”,“/home”).permitAll()
    ,以保护所有URL并将其重定向到登录

移除
antMatchers(“/”,“/home”).permitAll()
,这样每个访问都将重定向到`login@KavithakaranKanapathippillai可以因此,删除了该行,登录仍然以一个永无止境的登录循环返回到自身。@KavithakaranKanapathippillai相同的结果。@KavithakaranKanapathippillai我刚刚关闭Intelij并重新启动它,它就工作了。。。不知道为什么。非常感谢您抽出时间。
   protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .logout()
                .permitAll();
    }