• 4.2 实现注册与登录模块


    😊如果写的可以帮到你,可以点个赞吗,你的支持就是我写下去的动力。😊

    本文阅读大概 30 分钟, 自己写加思考大概 3 ~ 5 小时。建议:代码可以手抄, 但不要复制。


    1. 整体框架

    4.2 实现注册与登录模块


    2. 实现JwtToken验证


    2.1 添加依赖

    pom.xml 中添加下列依赖:

    jjwt-api
    jjwt-impl
    jjwt-jackson

    添加之后点击重新加载。

    <dependency>
        <groupId>io.jsonwebtokengroupId>
        <artifactId>jjwt-apiartifactId>
        <version>0.11.2version>
    dependency>
    <dependency>
        <groupId>io.jsonwebtokengroupId>
        <artifactId>jjwt-implartifactId>
        <version>0.11.2version>
        <scope>runtimescope>
    dependency>
    <dependency>
        <groupId>io.jsonwebtokengroupId>
        <artifactId>jjwt-jacksonartifactId>
        <version>0.11.2version>
        <scope>runtimescope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    如下图所示:

    jwt添加依赖


    2.2 编写、修改相关类

    1. 实现 JwtUtil

    backend 目录下创建软件包 utils 并创建 JwtUtil 类。
    JwtUtil 类为jwt 工具类,用来创建、解析 jwt token

    package com.kob.backend.utils;
    
    import io.jsonwebtoken.Claims;
    import io.jsonwebtoken.JwtBuilder;
    import io.jsonwebtoken.Jwts;
    import io.jsonwebtoken.SignatureAlgorithm;
    import org.springframework.stereotype.Component;
    
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    import java.util.Base64;
    import java.util.Date;
    import java.util.UUID;
    
    @Component
    public class JwtUtil {
        public static final long JWT_TTL = 60 * 60 * 1000L * 24 * 14;  // 有效期14天
        public static final String JWT_KEY = "SDFGjhdsfalshdfHFdsjkdsfds121232131afasdfac";
    
        public static String getUUID() {
            return UUID.randomUUID().toString().replaceAll("-", "");
        }
    
        public static String createJWT(String subject) {
            JwtBuilder builder = getJwtBuilder(subject, null, getUUID());
            return builder.compact();
        }
    
        private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {
            SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
            SecretKey secretKey = generalKey();
            long nowMillis = System.currentTimeMillis();
            Date now = new Date(nowMillis);
            if (ttlMillis == null) {
                ttlMillis = JwtUtil.JWT_TTL;
            }
    
            long expMillis = nowMillis + ttlMillis;
            Date expDate = new Date(expMillis);
            return Jwts.builder()
                    .setId(uuid)
                    .setSubject(subject)
                    .setIssuer("sg")
                    .setIssuedAt(now)
                    .signWith(signatureAlgorithm, secretKey)
                    .setExpiration(expDate);
        }
    
        public static SecretKey generalKey() {
            byte[] encodeKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
            return new SecretKeySpec(encodeKey, 0, encodeKey.length, "HmacSHA256");
        }
    
        public static Claims parseJWT(String jwt) throws Exception {
            SecretKey secretKey = generalKey();
            return Jwts.parserBuilder()
                    .setSigningKey(secretKey)
                    .build()
                    .parseClaimsJws(jwt)
                    .getBody();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    jwt-utils


    2. 实现 JwtAuthenticationTokenFilter

    backendconfig 目录下创建 cinfig 软件包,并创建 JwtAuthenticationTokenFilter 类。
    实现 JwtAuthenticationTokenFilter 类,用来验证 jwt token ,如果验证成功,则将 User 信息注入上下文中。

    package com.kob.backend.config.filter;
    
    
    import com.kob.backend.mapper.UserMapper;
    import com.kob.backend.pojo.User;
    import com.kob.backend.service.impl.utils.UserDetailsImpl;
    import com.kob.backend.utils.JwtUtil;
    import com.sun.istack.internal.NotNull;
    import io.jsonwebtoken.Claims;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringUtils;
    import org.springframework.web.filter.OncePerRequestFilter;
    
    import javax.servlet.FilterChain;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @Component
    public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
        @Autowired
        private UserMapper userMapper;
    
        @Override
        protected void doFilterInternal(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull FilterChain filterChain) throws ServletException, IOException {
            String token = request.getHeader("Authorization");
    
            if (!StringUtils.hasText(token) || !token.startsWith("Bearer ")) {
                filterChain.doFilter(request, response);
                return;
            }
    
            token = token.substring(7);
    
            String userid;
            try {
                Claims claims = JwtUtil.parseJWT(token);
                userid = claims.getSubject();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
    
            User user = userMapper.selectById(Integer.parseInt(userid));
    
            if (user == null) {
                throw new RuntimeException("用户名未登录");
            }
    
            UserDetailsImpl loginUser = new UserDetailsImpl(user);
            UsernamePasswordAuthenticationToken authenticationToken =
                    new UsernamePasswordAuthenticationToken(loginUser, null, null);
    
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
    
            filterChain.doFilter(request, response);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    在这里插入图片描述


    3. 配置config.SecurityConfig

    放行登录、注册等接口。

    package com.kob.backend.config;
    
    import com.kob.backend.config.filter.JwtAuthenticationTokenFilter;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.HttpMethod;
    import org.springframework.security.authentication.AuthenticationManager;
    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.config.http.SessionCreationPolicy;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.security.crypto.password.PasswordEncoder;
    import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
    
    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Autowired
        private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }
    
        @Bean
        @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
            return super.authenticationManagerBean();
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    .authorizeRequests()
                    .antMatchers("/user/account/token/", "/user/account/register/").permitAll()
                    .antMatchers(HttpMethod.OPTIONS).permitAll()
                    .anyRequest().authenticated();
    
            http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    security-config


    3. 实现后端API

    3.1 修改数据库

    • 将数据库中的 id 域变为自增。
    • pojo.User 类中添加注解:@TableId(type = IdType.AUTO)

    右击 user ,点击 修改表 ,双击 id ,选择 自动增加。同时添加一列 photo,类型设置为 varchar(1000) ,用来存储照片,默认值可以设置为自己的头像。


    修改数据库


    修改 User ,添加 photo

    package com.kob.backend.pojo;
    //与数据库中的表对应
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class User {
        @TableId(type = IdType.AUTO)
        private Integer id;
        private String username;
        private String password;
        private String photo;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    修改user


    3.2 实现接口API

    实现API需要三个相关类或接口:

    • service 下创建一个接口。
    • service 下创建一个类实现这个接口。
    • controller 下创建一个类进行操作。
    1. 实现 LoginService

    验证用户名密码,验证成功后返回 jwt token(令牌)

    创建接口:在 service下 创建 user 创建 account 新建一个接口 LoginService

    package com.kob.backend.service.user.account;
    
    import java.util.Map;
    
    public interface LoginService {
        public Map<String, String> getToken(String username, String password);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    login接口


    创建实现类:在 serviceimpl 下创建 user 创建 account 新建一个实现类LoginServiceImpl

    package com.kob.backend.service.impl.user.account;
    
    import com.kob.backend.pojo.User;
    import com.kob.backend.service.impl.utils.UserDetailsImpl;
    import com.kob.backend.service.user.account.LoginService;
    import com.kob.backend.utils.JwtUtil;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties;
    import org.springframework.security.authentication.AuthenticationManager;
    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    import org.springframework.security.core.Authentication;
    import org.springframework.stereotype.Service;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @Service
    public class LoginServiceImpl implements LoginService {
    
        @Autowired
        private AuthenticationManager authenticationManager;
    
        @Override
        public Map<String, String> getToken(String username, String password) {
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
    
            Authentication authenticate = authenticationManager.authenticate(authenticationToken);
            UserDetailsImpl loginUser = (UserDetailsImpl) authenticate.getPrincipal();
            User user = loginUser.getUser();
            String jwt = JwtUtil.createJWT(user.getId().toString());
    
            Map<String, String> map = new HashMap<>();
            map.put("error_message", "success");
            map.put("token", jwt);
    
            return map;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    login实现类


    创建controller:在 controller 创建 user 创建 account 新建一个LoginController

    package com.kob.backend.controller.user.account;
    
    import com.kob.backend.service.user.account.LoginService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Map;
    
    @RestController
    public class LoginController {
    
        @Autowired
        private LoginService loginService;
    
        @PostMapping("/user/account/token/")
        public Map<String, String> getToken(@RequestParam Map<String, String> map) {
            String username = map.get("username");
            String password = map.get("password");
    
            System.out.println(username + ' ' + password);
            return loginService.getToken(username, password);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    在这里插入图片描述


    2. 配置InforService

    根据令牌返回用户信息。

    创建接口:在 service下 创建 user 创建 account 新建一个接口 InfoService

    package com.kob.backend.service.user.account;
    
    import java.util.Map;
    
    public interface InfoService {
        public Map<String, String> getInfo();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述


    创建实现类:在 serviceimpl 下创建 user 创建 account 新建一个实现类InfoServiceImpl

    package com.kob.backend.service.impl.user.account;
    
    import com.kob.backend.pojo.User;
    import com.kob.backend.service.impl.utils.UserDetailsImpl;
    import com.kob.backend.service.user.account.InfoService;
    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.stereotype.Service;
    
    import java.util.HashMap;
    import java.util.Map;
    @Service
    public class InfoServiceImpl implements InfoService {
    
        /**
         * 根据token返回用户信息
         * @return map存储的信息
         */
        @Override
        public Map<String, String> getInfo() {
            UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
    
            UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();
            User user = loginUser.getUser();
    
            Map<String, String> map = new HashMap<>();
            map.put("error_message", "success");
            map.put("id", user.getId().toString());
            map.put("username", user.getUsername());
            map.put("photo", user.getPhoto());
            return map;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述


    创建controller:在 controller 创建 user 创建 account 新建一个InfoController

    package com.kob.backend.controller.user.account;
    
    import com.kob.backend.service.user.account.InfoService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Map;
    
    @RestController
    public class InfoController {
    
        @Autowired
        private InfoService infoService;
    
        @GetMapping("/user/account/info/")
        public Map<String, String> getInfo() {
            return infoService.getInfo();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述


    3. 配置RegisterService

    注册账号

    创建接口:在 service下 创建 user 创建 account 新建一个接口 RegisterService

    package com.kob.backend.service.user.account;
    
    import java.util.Map;
    
    public interface RegisterService {
        public Map<String, String> register(String username, String password, String confirmedPassword);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述


    创建实现类:在 serviceimpl 下创建 user 创建 account 新建一个实现类RegisterServiceImpl

    package com.kob.backend.service.impl.user.account;
    
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.kob.backend.mapper.UserMapper;
    import com.kob.backend.pojo.User;
    import com.kob.backend.service.user.account.RegisterService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.crypto.password.PasswordEncoder;
    import org.springframework.stereotype.Service;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @Service
    public class RegisterServiceImpl implements RegisterService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Autowired
        private PasswordEncoder passwordEncoder;
    
        @Override
        public Map<String, String> register(String username, String password, String confirmedPassword) {
            Map<String, String> map = new HashMap<>();
            if (username == null) {
                map.put("error_message", "用户名不能为空");
                return map;
            }
    
            if (password == null || confirmedPassword == null) {
                map.put("error_message", "密码不能为空");
                return map;
            }
            //删除首尾的空白字符
            username = username.trim();
            if (username.length() == 0) {
                map.put("error_message", "用户名不能为空");
                return map;
            }
    
            if (password.length() == 0 || confirmedPassword.length() == 0) {
                map.put("error_message", "密码不能为空");
                return map;
            }
    
            if (username.length() > 100) {
                map.put("error_message", "用户名长度不能大于100");
                return map;
            }
    
            if (password.length() > 100 || confirmedPassword.length() > 100) {
                map.put("error_message", "密码不能大于100");
                return map;
            }
    
            if (!password.equals(confirmedPassword)) {
                map.put("error_message", "两次输入的密码不一致");
                return map;
            }
    
            //查询用户名是否重复
            QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
            queryWrapper.eq("username", username);
            List<User> users = userMapper.selectList(queryWrapper);
            if (!users.isEmpty()) {
                map.put("error_message", "用户名已存在");
                return map;
            }
    
            // 添加一个新用户
            String encodedPassword = passwordEncoder.encode(password);
            //输入自己的图片地址
            String photo = "******************************";
            User user = new User(null, username, encodedPassword, photo);
            userMapper.insert(user);
    
            map.put("error_message", "success");
            return map;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    在这里插入图片描述


    创建controller:在 controller 创建 user 创建 account 新建一个RegisterController

    package com.kob.backend.controller.user.account;
    
    import com.kob.backend.service.user.account.RegisterService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Map;
    
    @RestController
    public class RegisterController {
    
        @Autowired
        private RegisterService registerService;
    
        @PostMapping("/user/account/register/")
        public Map<String, String> register(@RequestParam Map<String, String> map) {
            String username = map.get("username");
            String password = map.get("password");
            String confirmedPassword = map.get("confirmedPassword");
            return registerService.register(username, password, confirmedPassword);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述


    3.2 调试接口API

    验证用户登陆:

    APP.vue 中编写:

    注意:这里的Authorization: "Bearer "有空格,且token为自己的浏览器的token,需要更改token。

    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    在这里插入图片描述


    成功界面如下:


    测试成功


    4. 实现前端的登陆、注册界面

    1. 实现两个页面–登陆、注册的前端样式:

    bootstrap 上去寻找合适的样式:

    举个例子:

    样式:

    <div class="container">
      <div class="row row-cols-2">
        <div class="col">Columndiv>
        <div class="col">Columndiv>
      div>
    div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述


    表单:

    <div class="mb-3">
      <label for="exampleFormControlInput1" class="form-label">Email addresslabel>
      <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="name@example.com">
    div>
    <div class="mb-3">
      <label for="exampleFormControlTextarea1" class="form-label">Example textarealabel>
      <textarea class="form-control" id="exampleFormControlTextarea1" rows="3">textarea>
    div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述


    提交按钮

    <button type="button" class="btn btn-primary">Primarybutton>
    
    • 1

    创建页面:

    views 目录下创建 user ,新建两个文件 UserAccountLoginView.vueUserAccountRegisterView.vue

    实现 UserAccountRegisterView.vue 页面

    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述


    实现 UserAccountLoginView.vue 页面

    基本的 login 页面

    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    效果如下:


    在这里插入图片描述


    2. 在router目录下的index.js中加入路径

    修改 index.js

    import UserAccountLoginView from '../views/user/account/UserAccountLoginView'
    import UserAccountRegisterView from '../views/user/account/UserAccountRegisterView'
    
    const routes = [
      {
        path: "/user/account/login",
        name: "user_account_login",
        component: UserAccountLoginView,
      },
      {
        path: "/user/account/register",
        name: "user_account_register",
        component: UserAccountRegisterView,
      }
    
    ]
    
    const router = createRouter({
      history: createWebHistory(),
      routes
    })
    
    export default router
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述


    3. 在store下的user.js存储用户信息

    store 下创建 user.js

    import $ from 'jquery'
    
    export default {
        state: {
            id: "",
            username: "",
            password: "",
            photo: "",
            token: "",
            is_login: false,
        },
        getters: {
        },
        mutations: {
            updateUser(state, user) {
                state.id = user.id;
                state.username = user.username;
                state.photo = user.photo;
                state.is_login = user.is_login;
            },
            updateToken(state, token) {
                state.token = token;
            },
        },
        actions: {
            
        },
        modules: {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述


    4 把user加入到全局module

    store 下的 index.js

    import { createStore } from 'vuex'
    import ModuleUser from './user'
    
    export default createStore({
      state: {
      },
      getters: {
      },
      mutations: {
      },
      actions: {
      },
      modules: {
        user: ModuleUser,
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述


    5. 添加辅助函数login

    store 下的 user.js 修改。

    import $ from 'jquery'
    
    export default {
        state: {
            id: "",
            username: "",
            password: "",
            photo: "",
            token: "",
            is_login: false,
        },
        getters: {
        },
        mutations: {
            updateUser(state, user) {
                state.id = user.id;
                state.username = user.username;
                state.photo = user.photo;
                state.is_login = user.is_login;
            },
            updateToken(state, token) {
                state.token = token;
            },
        },
        actions: {
            login(context, data) {
                $.ajax({
                    url: "http://127.0.0.1:8080/user/account/token/",
                    type: "post",
                    data: {
                      username: data.username,
                      password: data.password,
                    },
                    success(resp) {
                        if (resp.error_message === "success") {
                            context.commit("updateToken", resp.token);
                            data.success(resp);
                        } else {
                            data.error(resp);
                        }
                    },
                    error(resp) {
                      data.error(resp); 
                    }
                  });
            },
        },
        modules: {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    在这里插入图片描述


    6. 在 UserAccountLoginView.vue 中实现

    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    在这里插入图片描述


    7. 把内容和变量绑定起来

    {{ error_message }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述


    8. 在登陆界面测试

    输入用户名和密码,获取 token


    在这里插入图片描述


    9. 用户名和密码输入正确,点击提交跳到主页面

    UserAccountLoginView.vue 中实现

    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    在这里插入图片描述


    10. 登陆成功后获取用户的信息

    user.js 中添加辅助函数

    import $ from 'jquery'
    
    export default {
        state: {
            id: "",
            username: "",
            password: "",
            photo: "",
            token: "",
            is_login: false,
        },
        getters: {
        },
        mutations: {
            updateUser(state, user) {
                state.id = user.id;
                state.username = user.username;
                state.photo = user.photo;
                state.is_login = user.is_login;
            },
            updateToken(state, token) {
                state.token = token;
            },
        },
        actions: {
            login(context, data) {
                $.ajax({
                    url: "http://127.0.0.1:8080/user/account/token/",
                    type: "post",
                    data: {
                      username: data.username,
                      password: data.password,
                    },
                    success(resp) {
                        if (resp.error_message === "success") {
                            context.commit("updateToken", resp.token);
                            data.success(resp);
                        } else {
                            data.error(resp);
                        }
                    },
                    error(resp) {
                      data.error(resp); 
                    }
                  });
            },
            getinfo(context, data) {
                $.ajax({
                    url: "http://127.0.0.1:8080/user/account/info/",
                    type: "get",
                    headers: {
                      Authorization: "Bearer " + context.state.token,
                    },
                    //成功就更新用户。
                    success(resp) {
                        if (resp.error_message === "success") {
                            // context.commit("updateToken", resp.token);
                            // data.success(resp);
                            context.commit("updateUser", {
                                ...resp,
                                is_login: true,
                            });
                        } else {
                            data.error(resp);
                        }
                    },
                    error(resp) {
                        data.error(resp);
                    }
                  });
            },
            logout(context) {
                context.commit("logout");
            }
        },
        modules: {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    在这里插入图片描述


    修改 UserAccountLoginView.vue

    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    在这里插入图片描述


    成功界面如下:


    在这里插入图片描述


    11. 把结果返回到右上角个人信息 – 修改导航栏

    修改 NavBar.vue

    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    在这里插入图片描述


    12. 退出登陆

    user.js 添加辅助函数

    mutations: {
        logout(state) {
            state.id = "";
            state.username = "";
            state.photo = "";
            state.token = "";
            state.is_login = false;
        }
    },
    actions: {
        logout(context) {
            context.commit("logout");
        }
    },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述


    navabar.vue 添加组件

  • 退出
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述


    代码地址

    4.2 实现注册与登录模块–King of Bots

  • 相关阅读:
    如何在PS(Adobe Photoshop)安装Portraiture3插件教程
    Java学习07——原码、反码和补码
    Win11切换输入法ctrl+shift没有反应怎么解决?
    谈一谈AI对人工的取代
    AI神器,逼真再现中秋诗句中的美景,震撼~
    日志pattern
    0830(042天 集合框架06 总结二)
    DSP开发入门
    IDEA 对单个的java class文件打成jar包
    mysql安装
  • 原文地址:https://blog.csdn.net/qq_41046821/article/details/126164643