From 1815a6329e1571703fde5f4a9393fa1a1c6b81e7 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sun, 16 Feb 2020 18:20:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configure/CustomWebSecurityConfig.java | 9 ++++++-- .../ase/controller/HomeController.java | 17 +++++++++++++- .../ase/controller/RegisterController.java | 13 ++++------- .../ase/model/permission/UserAuth.java | 7 ++++-- .../ase/model/permission/UserDetail.java | 3 --- .../ase/service/ASEUserDetailsService.java | 14 +++-------- .../codesdream/ase/service/IUserService.java | 5 +++- .../codesdream/ase/service/UserService.java | 23 +++++++++++++++---- src/main/resources/templates/login.html | 4 ++-- src/main/resources/templates/register.html | 4 ++-- 10 files changed, 61 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java b/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java index ef67a73..a94c16b 100644 --- a/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java +++ b/src/main/java/com/codesdream/ase/configure/CustomWebSecurityConfig.java @@ -1,6 +1,7 @@ package com.codesdream.ase.configure; import com.codesdream.ase.component.ASEPasswordEncoder; +import com.codesdream.ase.component.ASESecurityAuthenticationProvider; import com.codesdream.ase.service.ASEUserDetailsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; @@ -26,6 +27,9 @@ public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter { @Resource ASEPasswordEncoder asePasswordEncoder; + @Resource + ASESecurityAuthenticationProvider aseSecurityAuthenticationProvider; + @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() @@ -34,7 +38,7 @@ public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter { .csrf().disable().formLogin() .and() .formLogin().loginPage("/login") - .permitAll().defaultSuccessUrl("/").permitAll() + .permitAll().defaultSuccessUrl("/home").permitAll() .and() .logout().permitAll(); @@ -42,7 +46,8 @@ public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.userDetailsService(aseUserDetailService) + auth.authenticationProvider(aseSecurityAuthenticationProvider) + .userDetailsService(aseUserDetailService) .passwordEncoder(asePasswordEncoder); } diff --git a/src/main/java/com/codesdream/ase/controller/HomeController.java b/src/main/java/com/codesdream/ase/controller/HomeController.java index 2515570..858007b 100644 --- a/src/main/java/com/codesdream/ase/controller/HomeController.java +++ b/src/main/java/com/codesdream/ase/controller/HomeController.java @@ -1,12 +1,27 @@ package com.codesdream.ase.controller; +import com.codesdream.ase.model.permission.User; +import com.codesdream.ase.service.IUserService; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; +import javax.annotation.Resource; +import java.security.Principal; +import java.util.Optional; + @Controller public class HomeController { + @Resource + IUserService userService; + @RequestMapping(value = "/home") - public String showHomeView(){ + public String showHomeView(Model model, Principal principal){ + User user = userService.findUserByUsername(principal.getName()); + // 为视图模板指定参数 + model.addAttribute("username", user.getUsername().substring(0, 18)); + model.addAttribute("student_id", user.getUserAuth().getStudentID()); + model.addAttribute("is_at_school", user.getUserDetail().isAtSchool()); return "home"; } } diff --git a/src/main/java/com/codesdream/ase/controller/RegisterController.java b/src/main/java/com/codesdream/ase/controller/RegisterController.java index 024a7e4..248abd0 100644 --- a/src/main/java/com/codesdream/ase/controller/RegisterController.java +++ b/src/main/java/com/codesdream/ase/controller/RegisterController.java @@ -1,22 +1,15 @@ package com.codesdream.ase.controller; -import com.codesdream.ase.component.ASEPasswordEncoder; import com.codesdream.ase.model.permission.User; -import com.codesdream.ase.repository.UserRepository; import com.codesdream.ase.service.UserService; -import org.springframework.boot.autoconfigure.security.SecurityProperties; -import org.springframework.http.HttpRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.HttpRequestHandler; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.util.Map; -import java.util.Objects; @Controller public class RegisterController { @@ -34,14 +27,16 @@ public class RegisterController { Map parameterMap= request.getParameterMap(); // 进行处理前的检查 - if(parameterMap.containsKey("username") + if(parameterMap.containsKey("student-id") && parameterMap.containsKey("password") && parameterMap.containsKey("retry-password") && parameterMap.containsKey("user-question") && parameterMap.containsKey("user-answer") ) { User user = userService.getDefaultUser(); - user.setUsername(parameterMap.get("username")[0].toString()); + String student_id = parameterMap.get("student-id")[0].toString(); + // 生成随机用户名 + userService.generateRandomUsernameByStudentID(user, student_id); String password = parameterMap.get("password")[0].toString(); String retry_password = parameterMap.get("retry-password")[0].toString(); diff --git a/src/main/java/com/codesdream/ase/model/permission/UserAuth.java b/src/main/java/com/codesdream/ase/model/permission/UserAuth.java index e80d7b7..13e7071 100644 --- a/src/main/java/com/codesdream/ase/model/permission/UserAuth.java +++ b/src/main/java/com/codesdream/ase/model/permission/UserAuth.java @@ -14,11 +14,14 @@ public class UserAuth { private int id; // 密保问题 - private String user_question; + private String userQuestion; // 密保问题答案 - private String user_answer; + private String userAnswer; // 用户邮箱 private String mail; + + // 学生ID + private String studentID; } diff --git a/src/main/java/com/codesdream/ase/model/permission/UserDetail.java b/src/main/java/com/codesdream/ase/model/permission/UserDetail.java index 8e4fe06..148a185 100644 --- a/src/main/java/com/codesdream/ase/model/permission/UserDetail.java +++ b/src/main/java/com/codesdream/ase/model/permission/UserDetail.java @@ -33,9 +33,6 @@ public class UserDetail { // 真实姓名 private String realName; - // 学生ID - private String studentID; - // 在校状态 private boolean atSchool; } diff --git a/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java b/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java index aea8bc3..00e9ad8 100644 --- a/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java +++ b/src/main/java/com/codesdream/ase/service/ASEUserDetailsService.java @@ -23,16 +23,8 @@ public class ASEUserDetailsService implements UserDetailsService { @Override @Transactional public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { - System.out.println("User: " + s); - Optional user = userService.findUserByUsername(s); - if(!user.isPresent()){ - throw new UsernameNotFoundException("User Not Found"); - } - else { - System.out.println("Returning user information"); - System.out.println("User Password: "+user.get().getPassword()); - user.get().setAuthorities(userAuthoritiesGenerator.grantedAuthorities(user.get())); - return user.get(); - } + User user = userService.findUserByUsername(s); + user.setAuthorities(userAuthoritiesGenerator.grantedAuthorities(user)); + return user; } } diff --git a/src/main/java/com/codesdream/ase/service/IUserService.java b/src/main/java/com/codesdream/ase/service/IUserService.java index ea92e36..5de817d 100644 --- a/src/main/java/com/codesdream/ase/service/IUserService.java +++ b/src/main/java/com/codesdream/ase/service/IUserService.java @@ -16,7 +16,7 @@ public interface IUserService { List findAll(); Optional findUserById(int id); - Optional findUserByUsername(String username); + User findUserByUsername(String username); // 获得用户所有的权限角色 Collection getUserAuthorities(User user); @@ -24,6 +24,9 @@ public interface IUserService { // 更新用户的密码 void updatePassword(User user, String password); + // 生成随机用户名 + void generateRandomUsernameByStudentID(User user, String id); + User save(User user); User update(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 65ffbbf..45fb059 100644 --- a/src/main/java/com/codesdream/ase/service/UserService.java +++ b/src/main/java/com/codesdream/ase/service/UserService.java @@ -1,10 +1,12 @@ package com.codesdream.ase.service; import com.codesdream.ase.component.ASEPasswordEncoder; +import com.codesdream.ase.component.ASEUsernameEncoder; import com.codesdream.ase.component.UserRolesListGenerator; import com.codesdream.ase.model.permission.User; import com.codesdream.ase.repository.UserRepository; import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -21,7 +23,10 @@ public class UserService implements IUserService { UserRepository userRepository; @Resource - ASEPasswordEncoder asePasswordEncoder; + ASEPasswordEncoder passwordEncoder; + + @Resource + ASEUsernameEncoder usernameEncoder; @Override public List findAll() { @@ -34,8 +39,10 @@ public class UserService implements IUserService { } @Override - public Optional findUserByUsername(String username) { - return userRepository.findByUsername(username); + public User findUserByUsername(String username) { + Optional user = userRepository.findByUsername(username); + if(!user.isPresent()) throw new UsernameNotFoundException("User Not Found"); + return user.get(); } @Override @@ -45,16 +52,22 @@ public class UserService implements IUserService { @Override public void updatePassword(User user, String password) { - user.setPassword(asePasswordEncoder.encode(password)); + user.setPassword(passwordEncoder.encode(password)); update(user); } + @Override + public void generateRandomUsernameByStudentID(User user, String id) { + user.getUserAuth().setStudentID(id); + user.setUsername(usernameEncoder.encode(id)); + } + @Override public User save(User user) { // 查找用户名是否已经被注册 if(userRepository.findByUsername(user.getUsername()).isPresent()) throw new RuntimeException("Username Already Exists"); - user.setPassword(asePasswordEncoder.encode(user.getPassword())); + user.setPassword(passwordEncoder.encode(user.getPassword())); return userRepository.save(user); } diff --git a/src/main/resources/templates/login.html b/src/main/resources/templates/login.html index 9c21e7f..bab0b38 100644 --- a/src/main/resources/templates/login.html +++ b/src/main/resources/templates/login.html @@ -17,8 +17,8 @@
- - + +
diff --git a/src/main/resources/templates/register.html b/src/main/resources/templates/register.html index f9dc699..ceb1ccd 100644 --- a/src/main/resources/templates/register.html +++ b/src/main/resources/templates/register.html @@ -17,8 +17,8 @@
- - + +