代码调整

This commit is contained in:
Saturneric 2020-02-16 18:20:37 +08:00
parent 00d626d394
commit 1815a6329e
10 changed files with 61 additions and 38 deletions

View File

@ -1,6 +1,7 @@
package com.codesdream.ase.configure; package com.codesdream.ase.configure;
import com.codesdream.ase.component.ASEPasswordEncoder; import com.codesdream.ase.component.ASEPasswordEncoder;
import com.codesdream.ase.component.ASESecurityAuthenticationProvider;
import com.codesdream.ase.service.ASEUserDetailsService; import com.codesdream.ase.service.ASEUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -26,6 +27,9 @@ public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Resource @Resource
ASEPasswordEncoder asePasswordEncoder; ASEPasswordEncoder asePasswordEncoder;
@Resource
ASESecurityAuthenticationProvider aseSecurityAuthenticationProvider;
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests() http.authorizeRequests()
@ -34,7 +38,7 @@ public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter {
.csrf().disable().formLogin() .csrf().disable().formLogin()
.and() .and()
.formLogin().loginPage("/login") .formLogin().loginPage("/login")
.permitAll().defaultSuccessUrl("/").permitAll() .permitAll().defaultSuccessUrl("/home").permitAll()
.and() .and()
.logout().permitAll(); .logout().permitAll();
@ -42,7 +46,8 @@ public class CustomWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(aseUserDetailService) auth.authenticationProvider(aseSecurityAuthenticationProvider)
.userDetailsService(aseUserDetailService)
.passwordEncoder(asePasswordEncoder); .passwordEncoder(asePasswordEncoder);
} }

View File

@ -1,12 +1,27 @@
package com.codesdream.ase.controller; 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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.security.Principal;
import java.util.Optional;
@Controller @Controller
public class HomeController { public class HomeController {
@Resource
IUserService userService;
@RequestMapping(value = "/home") @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"; return "home";
} }
} }

View File

@ -1,22 +1,15 @@
package com.codesdream.ase.controller; package com.codesdream.ase.controller;
import com.codesdream.ase.component.ASEPasswordEncoder;
import com.codesdream.ase.model.permission.User; import com.codesdream.ase.model.permission.User;
import com.codesdream.ase.repository.UserRepository;
import com.codesdream.ase.service.UserService; 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.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Map; import java.util.Map;
import java.util.Objects;
@Controller @Controller
public class RegisterController { public class RegisterController {
@ -34,14 +27,16 @@ public class RegisterController {
Map<String, String[]> parameterMap= request.getParameterMap(); Map<String, String[]> parameterMap= request.getParameterMap();
// 进行处理前的检查 // 进行处理前的检查
if(parameterMap.containsKey("username") if(parameterMap.containsKey("student-id")
&& parameterMap.containsKey("password") && parameterMap.containsKey("password")
&& parameterMap.containsKey("retry-password") && parameterMap.containsKey("retry-password")
&& parameterMap.containsKey("user-question") && parameterMap.containsKey("user-question")
&& parameterMap.containsKey("user-answer") && parameterMap.containsKey("user-answer")
) { ) {
User user = userService.getDefaultUser(); 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 password = parameterMap.get("password")[0].toString();
String retry_password = parameterMap.get("retry-password")[0].toString(); String retry_password = parameterMap.get("retry-password")[0].toString();

View File

@ -14,11 +14,14 @@ public class UserAuth {
private int id; private int id;
// 密保问题 // 密保问题
private String user_question; private String userQuestion;
// 密保问题答案 // 密保问题答案
private String user_answer; private String userAnswer;
// 用户邮箱 // 用户邮箱
private String mail; private String mail;
// 学生ID
private String studentID;
} }

View File

@ -33,9 +33,6 @@ public class UserDetail {
// 真实姓名 // 真实姓名
private String realName; private String realName;
// 学生ID
private String studentID;
// 在校状态 // 在校状态
private boolean atSchool; private boolean atSchool;
} }

View File

@ -23,16 +23,8 @@ public class ASEUserDetailsService implements UserDetailsService {
@Override @Override
@Transactional @Transactional
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
System.out.println("User: " + s); User user = userService.findUserByUsername(s);
Optional<User> user = userService.findUserByUsername(s); user.setAuthorities(userAuthoritiesGenerator.grantedAuthorities(user));
if(!user.isPresent()){ return user;
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();
}
} }
} }

View File

@ -16,7 +16,7 @@ public interface IUserService {
List<User> findAll(); List<User> findAll();
Optional<User> findUserById(int id); Optional<User> findUserById(int id);
Optional<User> findUserByUsername(String username); User findUserByUsername(String username);
// 获得用户所有的权限角色 // 获得用户所有的权限角色
Collection<? extends GrantedAuthority> getUserAuthorities(User user); Collection<? extends GrantedAuthority> getUserAuthorities(User user);
@ -24,6 +24,9 @@ public interface IUserService {
// 更新用户的密码 // 更新用户的密码
void updatePassword(User user, String password); void updatePassword(User user, String password);
// 生成随机用户名
void generateRandomUsernameByStudentID(User user, String id);
User save(User user); User save(User user);
User update(User user); User update(User user);

View File

@ -1,10 +1,12 @@
package com.codesdream.ase.service; package com.codesdream.ase.service;
import com.codesdream.ase.component.ASEPasswordEncoder; import com.codesdream.ase.component.ASEPasswordEncoder;
import com.codesdream.ase.component.ASEUsernameEncoder;
import com.codesdream.ase.component.UserRolesListGenerator; import com.codesdream.ase.component.UserRolesListGenerator;
import com.codesdream.ase.model.permission.User; import com.codesdream.ase.model.permission.User;
import com.codesdream.ase.repository.UserRepository; import com.codesdream.ase.repository.UserRepository;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -21,7 +23,10 @@ public class UserService implements IUserService {
UserRepository userRepository; UserRepository userRepository;
@Resource @Resource
ASEPasswordEncoder asePasswordEncoder; ASEPasswordEncoder passwordEncoder;
@Resource
ASEUsernameEncoder usernameEncoder;
@Override @Override
public List<User> findAll() { public List<User> findAll() {
@ -34,8 +39,10 @@ public class UserService implements IUserService {
} }
@Override @Override
public Optional<User> findUserByUsername(String username) { public User findUserByUsername(String username) {
return userRepository.findByUsername(username); Optional<User> user = userRepository.findByUsername(username);
if(!user.isPresent()) throw new UsernameNotFoundException("User Not Found");
return user.get();
} }
@Override @Override
@ -45,16 +52,22 @@ public class UserService implements IUserService {
@Override @Override
public void updatePassword(User user, String password) { public void updatePassword(User user, String password) {
user.setPassword(asePasswordEncoder.encode(password)); user.setPassword(passwordEncoder.encode(password));
update(user); update(user);
} }
@Override
public void generateRandomUsernameByStudentID(User user, String id) {
user.getUserAuth().setStudentID(id);
user.setUsername(usernameEncoder.encode(id));
}
@Override @Override
public User save(User user) { public User save(User user) {
// 查找用户名是否已经被注册 // 查找用户名是否已经被注册
if(userRepository.findByUsername(user.getUsername()).isPresent()) if(userRepository.findByUsername(user.getUsername()).isPresent())
throw new RuntimeException("Username Already Exists"); throw new RuntimeException("Username Already Exists");
user.setPassword(asePasswordEncoder.encode(user.getPassword())); user.setPassword(passwordEncoder.encode(user.getPassword()));
return userRepository.save(user); return userRepository.save(user);
} }

View File

@ -17,8 +17,8 @@
<form action="/login" method="post"> <form action="/login" method="post">
<div class="card-body"> <div class="card-body">
<div class="form-group"> <div class="form-group">
<label for="username">用户名</label> <label for="username">学号</label>
<input type="text" class="form-control" id="username" name="username" placeholder="这里输入用户名" required> <input type="text" class="form-control" id="username" name="username" placeholder="这里输入学号" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="password">密码</label> <label for="password">密码</label>

View File

@ -17,8 +17,8 @@
<form action="/register" method="post"> <form action="/register" method="post">
<div class="card-body"> <div class="card-body">
<div class="form-group"> <div class="form-group">
<label for="username">用户名</label> <label for="student-id">学号</label>
<input type="text" class="form-control" id="username" name="username" placeholder="这里输入用户名"> <input type="text" class="form-control" id="student-id" name="student-id" placeholder="这里输入学号">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="password">密码</label> <label for="password">密码</label>