Skip to content

Commit

Permalink
增加登录页面
Browse files Browse the repository at this point in the history
  • Loading branch information
stronger1990 committed Feb 28, 2023
1 parent a918538 commit 7594e86
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 10 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.pjb.springbootjjwt.mapper")
@SpringBootApplication
@MapperScan("com.pjb.springbootjjwt.mapper")
public class SpringbootJwtApplication {

public static void main(String[] args) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/pjb/springbootjjwt/api/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.pjb.springbootjjwt.api;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class LoginController {

@GetMapping(value = "/aaa")
public String getLoginPage() {
return "login";
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/pjb/springbootjjwt/api/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ public String testIfNormal() {
return "服务器能正常访问";
}

// 登录
/**
* 登录接口
* 一开始没有@RequestBody,所以拿到的user一直为null,因为JSON是放在body的
* @param user
* @return
*/
@PostMapping("/login")
public Object login(User user) {
public Object login(@RequestBody User user) {
JSONObject jsonObject = new JSONObject();
User userForBase = userService.findByUsername(user);
if (userForBase == null) {
Expand All @@ -44,6 +49,7 @@ public Object login(User user) {
String token = tokenService.getToken(userForBase);
jsonObject.put("token", token);
jsonObject.put("user", userForBase);
jsonObject.put("message", "登录成功");
return jsonObject;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/pjb/springbootjjwt/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
//@AllArgsConstructor
//@NoArgsConstructor
public class User {
private String Id;
private String id;
private String username;
private String password;

public String getId() {
return Id;
return id;
}

public void setId(String id) {
Id = id;
this.id = id;
}

public String getUsername() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletRespo
}
HandlerMethod handlerMethod = (HandlerMethod) object;
Method method = handlerMethod.getMethod();
// 检查是否有passtoken注释,有则跳过认证
// 检查是否有PassToken注释,判断是否为true则跳过认证
if (method.isAnnotationPresent(PassToken.class)) {
PassToken passToken = method.getAnnotation(PassToken.class);
if (passToken.required()) {
Expand Down Expand Up @@ -75,6 +75,7 @@ public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletRespo
return true;
}
}
// 如果没有PassToken和UserLoginToken注解,也默认能访问
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
*/
public interface UserMapper {
User findByUsername(@Param("username") String username);
User findUserById(@Param("Id") String Id);
User findUserById(@Param("id") String id);
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
debug: true
server:
port: 8888
port: 8123
2 changes: 1 addition & 1 deletion src/main/resources/mapper/UserMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
resultType="com.pjb.springbootjjwt.entity.User">
SELECT *
FROM user
where id = #{Id}
where id = #{id}
</select>
</mapper>
2 changes: 2 additions & 0 deletions src/main/resources/static/js/jquery-3.6.0.min.js

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登录测试</title>
<script src="js/jquery-3.6.0.min.js"></script>
</head>
<body>

<style type="text/css">
#div_bg {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #fff;
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-o-background-size: cover;
background-position: center 0;
background-attachment: fixed; /*全屏居中的关键*/
}

#button{
width:100px;
height: 100px;
background-color: palevioletred;
}
</style>

<script type="text/javascript">
function postJson() {
console.log('执行了postJson函数');
$.ajax({
url: 'http://localhost:8123/api/login',
type: 'POST',
data: '{"username":"strong", "password":"123456"}',
contentType: 'application/json',
success: function(data) {
alert(data.message);
}
});
}
</script>


<div id="div_bg">
<button type="button" onclick="postJson()">测试</button>
</div>

</body>
</html>

0 comments on commit 7594e86

Please sign in to comment.