前言:

本文内容:员工管理系统:国际化、登录功能实现、登录拦截器

推荐免费SpringBoot基础教程视频:【狂神说Java】SpringBoot最新教程通俗易懂_哔哩哔哩_bilibili

员工管理系统:国际化

  1. resources目录下新建i18n

  2. i18n下新建login.propertieslogin_zh_CN.propertieslogin_en_US.properties

    login.properties

    1
    2
    3
    4
    5
    login.btn=登录
    login.password=密码
    login.remeber=记住密码
    login.tip 请登录
    login.username=用户名

    login_zh_CN.properties

    1
    2
    3
    4
    5
    login.btn=登录
    login.password=密码
    login.remeber=记住密码
    login.tip=请登录
    login.username=用户名

    login_en_US.properties

    1
    2
    3
    4
    5
    login.btn=Sign in
    login.password=Password
    login.remeber=Remember me
    login.tip=Please sign in
    login.username=Username
  3. application.properties进行国际化配置

    1
    2
    # 存放国际化配置文件路径 国际化取值#{value}
    spring.messages.basename=i18n.login
  4. login.html页面进行取值

    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
    <!DOCTYPE html>
    <!--引入thymeleaf的约束-->
    <html lang="en" xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
    xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>首页</title>
    <!-- 通过 thymeleaf 语法引入链接-->
    <!-- Bootstrap core CSS -->
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link th:href="@{/css/signin.css}" rel="stylesheet">
    <!-- 自定义图标-->
    <link rel="icon" th:href="@{/favicon.ico}">
    </head>

    <body class="text-center">
    <form class="form-signin" action="dashboard.html">
    <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
    <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
    <label class="sr-only" th:text="#{login.username}">Username</label>
    <input type="text" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
    <label class="sr-only" th:text="#{login.password}">Password</label>
    <input type="password" class="form-control" th:placeholder="#{login.password}" required="">
    <div class="checkbox mb-3">
    <label>
    <input type="checkbox" value="remember-me" th:text="#{login.remeber}">
    </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
    <p class="mt-5 mb-3 text-muted">© 2017-2022</p>
    <a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
    <a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
    </form>
    </body>
    </html>
  5. com.jokerdig.config包下新建MyLocaleResolver.java

    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
    package com.jokerdig.config;

    import org.apache.tomcat.jni.Local;
    import org.springframework.web.servlet.LocaleResolver;
    import org.thymeleaf.util.StringUtils;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.util.Locale;

    /**
    * @author Joker大雄
    * @data 2022/7/19 - 14:58
    **/
    // 地区解析器
    public class MyLocaleResolver implements LocaleResolver {

    // 解析请求
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
    // 获取请求中的语言参数
    String language = request.getParameter("l");
    Locale locale = Locale.getDefault(); // 如果没有就默认
    // 如果不为空就使用我们传递的
    if(!StringUtils.isEmpty(language)){
    // zh_CN
    String[] split = language.split("_");
    // 国家 地区
    locale = new Locale(split[0],split[1]);
    return locale;
    }
    return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
    }
    }
  6. MyMVCConfig.java进行配置

    1
    2
    3
    4
    5
    // 自定义国际化组件
    @Bean
    public LocaleResolver localeResolver(){
    return new MyLocaleResolver();
    }
  7. 运行测试

    点击中文和English可以进行语言切换

  8. 小结

    • 需要配置i18n文件
    • 如果需要在项目中进行按钮自动切换,需要自定义国际化组件
    • 记得将自己写的组件配置到spring容器(@Bean)

员工管理系统:登录功能实现

  1. controller包下新建LoginController

    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
    package com.jokerdig.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.thymeleaf.util.StringUtils;

    /**
    * @author Joker大雄
    * @data 2022/7/19 - 15:17
    **/
    @Controller
    public class LoginController {

    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model){
    // 具体的业务
    if(!StringUtils.isEmpty(username) && "123456".equals(password)){
    return "redirect:/main.html";
    }else{
    model.addAttribute("msg","用户名或密码错误");
    }
    return "index";
    }
    }
  2. 修改login.html文件下的表单请求地址并给账号框和密码框添加name

    1
    2
    3
    <form class="form-signin" th:action="@{/user/login}">
    <input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
    <input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
  3. MyMVCConfig.java配置主页面映射

    1
    2
    3
    4
    5
    6
    7
    // 配置映射
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
    registry.addViewController("/index.html").setViewName("index");
    registry.addViewController("/main.html").setViewName("dashboard");
    }
  4. 运行测试

    点击登录后跳转到http://localhost:8080/main.html

  5. 登录功能实现

员工管理系统:登录拦截器

防止未登录的用户直接访问其他页面

  1. config包下新建LoginHandlerInterceptor.java

    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
    package com.jokerdig.config;

    import org.springframework.web.servlet.HandlerInterceptor;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    * @author Joker大雄
    * @data 2022/7/19 - 15:45
    **/
    public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // 登录成功之后,会有用户session
    Object loginUser = request.getSession().getAttribute("loginUser");

    if(loginUser==null){
    request.setAttribute("msg","仅限登录用户访问");
    request.getRequestDispatcher("/index.html").forward(request,response);
    return false;
    }else{
    return true;
    }
    }
    }
  2. 修改LoginController,添加session

    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
    package com.jokerdig.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.thymeleaf.util.StringUtils;

    import javax.servlet.http.HttpSession;

    /**
    * @author Joker大雄
    * @data 2022/7/19 - 15:17
    **/
    @Controller
    public class LoginController {

    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session){
    // 具体的业务
    if(!StringUtils.isEmpty(username) && "123456".equals(password)){
    session.setAttribute("loginUser",username);
    return "redirect:/main.html";
    }else{
    model.addAttribute("msg","用户名或密码错误");
    }
    return "index";
    }
    }
  3. MyMVCConfig中进行配置

    1
    2
    3
    4
    5
    // 登录拦截器配置
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login","/css/*","/js/*","/img/*","/favicon.ico");
    }
  4. 运行测试

    拦截器生效

  5. 登录拦截器完成