springboot security登录界面Please sign in

spring boot集成security,启动项目不是出了自己的登录页面

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


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



 

想要实时关注更多干货好文,扫描下图关注:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Security 提供了一个登录认证的框架,可以轻松地实现登录认证功能。下面是一个简单的 Spring Boot Security 的登录接口示例。 首先,需要在 `pom.xml` 文件中添加 Spring Boot Security 的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 然后,在 Spring Boot 应用程序的主类上添加 `@EnableWebSecurity` 注释来启用 Spring SecurityWeb 安全功能。并创建一个实现了 `UserDetailsService` 接口的类,用于获取用户信息。 ```java @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @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(); } } ``` 在登录页面需要提供一个表单,通过 POST 请求发送到 `/login` 接口。 ```html <form action="/login" method="post"> <div> <label>Username:</label> <input type="text" name="username"/> </div> <div> <label>Password:</label> <input type="password" name="password"/> </div> <div> <input type="submit" value="Log In"/> </div> </form> ``` 在服务器端,需要实现 `/login` 接口,用于接收登录请求并进行认证。 ```java @RestController public class LoginController { @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(@RequestParam String username, @RequestParam String password) { // 进行用户认证 Authentication authentication = new UsernamePasswordAuthenticationToken(username, password); Authentication result = authenticationManager.authenticate(authentication); SecurityContextHolder.getContext().setAuthentication(result); return "Success"; } } ``` 以上代码仅提供了一个简单的 Spring Boot Security 的登录接口示例,实际上还需要更多的代码来实现完整的安全功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唐 城

小朋友,你是不是有很多问号?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值