springboot security莫名其妙的登录界面Please sign in

spring boot集成security,启动项目不是出了自己的登录页面,而是下面莫名其妙的页面:

 

 

整了好久一直是这样,后来才发现其实这个是security默认给我们整的一个用户认证的功能,用户名是:user   密码是在启动的控制台打印出来的:

输入用户名和密码   登录进去报404   说明我们什么服务也没有配置,要想配置自己的认证   需要添加一个继承WebSecurityConfigurerAdapter这个适配器的一个配置类,如果说你已经添加配置类,这个页面恰恰说明你的配置类没有起作用,有可能是配置类里面没有添加 @Configuration 配置 。

  • 16
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
对于Spring Boot的登录界面,你可以使用Spring Security来实现。下面是一个简单的示例代码: 首先,在pom.xml文件中添加Spring Security的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 然后,创建一个登录页面的HTML模板(例如login.html),并放置在src/main/resources/templates目录下: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h1>Login</h1> <form action="/login" method="post"> <div> <label for="username">Username:</label> <input type="text" id="username" name="username"> </div> <div> <label for="password">Password:</label> <input type="password" id="password" name="password"> </div> <div> <button type="submit">Login</button> </div> </form> </body> </html> ``` 接下来,在Spring Boot应用程序的配置类(如Application.java)上添加@EnableWebSecurity注解,并实现一个WebSecurityConfigurerAdapter: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 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; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/login") .permitAll() .anyRequest() .authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/home") .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user") .password("{noop}password") // 添加 {noop} 前缀表示密码不加密,仅用于示例 .roles("USER"); } } ``` 在这个示例中,配置类中的configure方法定义了访问权限和登录页面的路径。configureGlobal方法定义了一个简单的用户认证。 最后,在控制器中添加处理登录请求的方法: ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class LoginController { @GetMapping("/login") public String login() { return "login"; } } ``` 这样就完成了一个简单的Spring Boot登录界面的实现。用户访问"/login"路径时会显示login.html页面,输入用户名和密码后可以登录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值