【SpringBoot】SpringBoot基础教程(8)
前言:
本文内容:员工管理系统:国际化、登录功能实现、登录拦截器
推荐免费SpringBoot基础教程视频:【狂神说Java】SpringBoot最新教程通俗易懂_哔哩哔哩_bilibili
员工管理系统:国际化
-
在
resources
目录下新建i18n
-
在
i18n
下新建login.properties
,login_zh_CN.properties
和login_en_US.properties
login.properties
1
2
3
4
5login.btn=登录
login.password=密码
login.remeber=记住密码
login.tip 请登录
login.username=用户名login_zh_CN.properties
1
2
3
4
5login.btn=登录
login.password=密码
login.remeber=记住密码
login.tip=请登录
login.username=用户名login_en_US.properties
1
2
3
4
5login.btn=Sign in
login.password=Password
login.remeber=Remember me
login.tip=Please sign in
login.username=Username -
在
application.properties
进行国际化配置1
2# 存放国际化配置文件路径 国际化取值#{value}
spring.messages.basename=i18n.login -
在
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
<!--引入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> -
在
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
38package 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 {
// 解析请求
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;
}
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
} -
在
MyMVCConfig.java
进行配置1
2
3
4
5// 自定义国际化组件
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
} -
运行测试
点击中文和English可以进行语言切换
-
小结
- 需要配置i18n文件
- 如果需要在项目中进行按钮自动切换,需要自定义国际化组件
- 记得将自己写的组件配置到spring容器(
@Bean
)
员工管理系统:登录功能实现
-
在
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
27package 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
**/
public class LoginController {
public String login({ String username, String password, Model model)
// 具体的业务
if(!StringUtils.isEmpty(username) && "123456".equals(password)){
return "redirect:/main.html";
}else{
model.addAttribute("msg","用户名或密码错误");
}
return "index";
}
} -
修改
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=""> -
在
MyMVCConfig.java
配置主页面映射1
2
3
4
5
6
7// 配置映射
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
} -
运行测试
点击登录后跳转到http://localhost:8080/main.html
-
登录功能实现
员工管理系统:登录拦截器
防止未登录的用户直接访问其他页面
-
在
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
27package 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 {
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;
}
}
} -
修改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
30package 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
**/
public class LoginController {
public String login({ String username, 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";
}
} -
在
MyMVCConfig
中进行配置1
2
3
4
5// 登录拦截器配置
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login","/css/*","/js/*","/img/*","/favicon.ico");
} -
运行测试
拦截器生效
-
登录拦截器完成
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Hey,Joker!
评论
ValineTwikoo