From 440c3da1a7a58a0ab3ecef9157375bdc39206695 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Fri, 31 Jan 2020 16:59:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E9=87=8F=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configure/CustomWebSecurityConfig.java | 22 +++++++ .../ase/controller/LoginController.java | 27 ++++++++ .../java/com/codesdream/ase/model/Tag.java | 13 ++++ .../java/com/codesdream/ase/model/User.java | 63 +++++++++++++++++++ .../ase/repository/UserRepository.java | 10 +++ .../codesdream/ase/service/IUserService.java | 14 +++++ .../codesdream/ase/service/UserService.java | 32 +++++++++- src/main/resources/templates/layout.html | 54 ++++++++-------- src/main/resources/templates/login.html | 41 ++++++++++++ 9 files changed, 248 insertions(+), 28 deletions(-) create mode 100644 src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java create mode 100644 src/main/java/com/codesdream/ase/controller/LoginController.java create mode 100644 src/main/java/com/codesdream/ase/model/Tag.java create mode 100644 src/main/java/com/codesdream/ase/model/User.java create mode 100644 src/main/java/com/codesdream/ase/repository/UserRepository.java create mode 100644 src/main/java/com/codesdream/ase/service/IUserService.java create mode 100644 src/main/resources/templates/login.html diff --git a/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java b/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java new file mode 100644 index 0000000..3ee8adb --- /dev/null +++ b/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java @@ -0,0 +1,22 @@ +package com.codesdream.ase.configure; + +import org.springframework.context.annotation.Configuration; +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; + +@Configuration +@EnableWebSecurity +public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + super.configure(http); +/* .authorizeRequests().anyRequest().permitAll()*/ +/* .antMatchers("/index", "/assets/**").permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage("/login") + .permitAll()*/; + } +} diff --git a/src/main/java/com/codesdream/ase/controller/LoginController.java b/src/main/java/com/codesdream/ase/controller/LoginController.java new file mode 100644 index 0000000..02ce7cc --- /dev/null +++ b/src/main/java/com/codesdream/ase/controller/LoginController.java @@ -0,0 +1,27 @@ +package com.codesdream.ase.controller; + +import com.codesdream.ase.model.User; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.util.DigestUtils; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import javax.servlet.http.HttpServletRequest; + +@Controller +public class LoginController { + @RequestMapping(value = "/login") + String printLogin(Model model){ + return "login"; + } + + @RequestMapping(value = "/login", method = RequestMethod.POST) + String checkLogin(Model model, HttpServletRequest request){ + User user = new User(); + user.setUsername(request.getParameter("username")); + user.setPassword(DigestUtils.md5DigestAsHex(request.getParameter("password").getBytes())); + return "login"; + } + +} diff --git a/src/main/java/com/codesdream/ase/model/Tag.java b/src/main/java/com/codesdream/ase/model/Tag.java new file mode 100644 index 0000000..69e9623 --- /dev/null +++ b/src/main/java/com/codesdream/ase/model/Tag.java @@ -0,0 +1,13 @@ +package com.codesdream.ase.model; + +import javax.persistence.*; + +@Entity +@Table(name = "user_tag") +public class Tag { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + private String name; + private String description; +} diff --git a/src/main/java/com/codesdream/ase/model/User.java b/src/main/java/com/codesdream/ase/model/User.java new file mode 100644 index 0000000..d2d4557 --- /dev/null +++ b/src/main/java/com/codesdream/ase/model/User.java @@ -0,0 +1,63 @@ +package com.codesdream.ase.model; + +import javax.persistence.*; +import java.util.Objects; + +@Entity +@Table(name = "user_login") +public class User { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + private String username; + private String password; + + public User(){ + + } + + public User(String username, String password) { + this.username = username; + this.password = password; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + User user = (User) o; + return id == user.id && + Objects.equals(username, user.username) && + Objects.equals(password, user.password); + } + + @Override + public int hashCode() { + return Objects.hash(id, username, password); + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", password='" + password + '\'' + + '}'; + } +} diff --git a/src/main/java/com/codesdream/ase/repository/UserRepository.java b/src/main/java/com/codesdream/ase/repository/UserRepository.java new file mode 100644 index 0000000..7c66b5b --- /dev/null +++ b/src/main/java/com/codesdream/ase/repository/UserRepository.java @@ -0,0 +1,10 @@ +package com.codesdream.ase.repository; + +import com.codesdream.ase.model.User; +import org.springframework.data.repository.CrudRepository; + +import java.util.Optional; + +public interface UserRepository extends CrudRepository { + Optional findByUsername(String username); +} diff --git a/src/main/java/com/codesdream/ase/service/IUserService.java b/src/main/java/com/codesdream/ase/service/IUserService.java new file mode 100644 index 0000000..1b81223 --- /dev/null +++ b/src/main/java/com/codesdream/ase/service/IUserService.java @@ -0,0 +1,14 @@ +package com.codesdream.ase.service; + +import com.codesdream.ase.model.User; + +import java.util.List; +import java.util.Optional; + +public interface IUserService { + List findAll(); + + Optional findUserById(long id); + Optional findUserByUsername(String username); + User save(User user); +} diff --git a/src/main/java/com/codesdream/ase/service/UserService.java b/src/main/java/com/codesdream/ase/service/UserService.java index 29518ef..dad4916 100644 --- a/src/main/java/com/codesdream/ase/service/UserService.java +++ b/src/main/java/com/codesdream/ase/service/UserService.java @@ -1,5 +1,35 @@ package com.codesdream.ase.service; -public interface UserService { +import com.codesdream.ase.model.User; +import com.codesdream.ase.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; +import java.util.Optional; + +public class UserService implements IUserService { + @Autowired + UserRepository userRepository; + + @Override + public List findAll() { + return (List) userRepository.findAll(); + } + + @Override + public Optional findUserById(long id) { + return userRepository.findById(id); + } + + @Override + public Optional findUserByUsername(String username) { + return userRepository.findByUsername(username); + } + + @Override + public User save(User user) { + return userRepository.save(user); + } + } diff --git a/src/main/resources/templates/layout.html b/src/main/resources/templates/layout.html index 34eaf89..ec15192 100644 --- a/src/main/resources/templates/layout.html +++ b/src/main/resources/templates/layout.html @@ -13,7 +13,7 @@ - - + + + - - + + - + - + - + - + - + - + - - + + - + - + - - + +