springboot实现简单的登陆

完整的项目结构:
在这里插入图片描述

资源文件application.properties

#操作数据库的完整配置
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.database=mysql
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
#Mybatis扫描
mybatis.mapper-locations=classpath*:mapper/*.xml
#起别名。可省略写mybatis的xml中的resultType的全路径
#扫包的方式配置mtbatis的别名
mybatis.type-aliases-package=com.example4.demo4.domain
#配置端口号
#页面热加载
spring.thymeleaf.cache=false
server.port=80
#配置页面跳转路径

页面默认前缀目录

#spring.mvc.view.prefix=/WEB-INF/test/

响应页面默认后缀

#spring.mvc.view.suffix=.jsp
#低版本的时候这么配置,但是这个项目搭建不是这个的原因
#spring.view.prefix=/WEB-INF/test/
#spring.view.suffix=.jsp
在这里插入图片描述

controller

在这里插入图片描述

service

在这里插入图片描述

mapper:

在这里插入图片描述

mapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example5.demo5.mapper.UserMapper" >
    <select id="checkLogin" resultType="User">
        SELECT * FROM t_login WHERE name=#{name} AND password=#{password}
    </select>
</mapper>

注:注意应用文件Demo4Application的位置,一定要能够扫描到所有的包

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现用户登录功能,可以使用Spring Boot框架中的Spring Security模块。下面是一个简单的示例代码,说明如何在Spring Boot中实现用户登录: 1. 添加依赖:在pom.xml文件中添加Spring Security的依赖。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 创建用户实体类:创建一个表示用户的实体类,包括用户名和密码等属性。 ```java @Entity @Table(name = "users") public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, unique = true) private String username; @Column(nullable = false) private String password; // 省略getter和setter方法 } ``` 3. 创建用户仓库:创建一个用于操作数据库的用户仓库接口。 ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { User findByUsername(String username); } ``` 4. 创建认证用户服务:创建一个实现了UserDetailsService接口的认证用户服务类。 ```java @Service public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("用户不存在"); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), new ArrayList<>()); } } ``` 5. 配置Spring Security:创建一个配置类来配置Spring Security。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login", "/register").permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage("/login").defaultSuccessUrl("/home").permitAll() .and() .logout().permitAll(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 在上述代码中,我们配置了登录页面为"/login",默认成功跳转页面为"/home",并设置了访问权限规则。 以上是使用Spring Boot实现用户登录的基本步骤,你可以根据自己的需求进行定制和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值