代码调整
This commit is contained in:
parent
00d626d394
commit
1815a6329e
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
@ -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<String, String[]> 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();
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -33,9 +33,6 @@ public class UserDetail {
|
||||
// 真实姓名
|
||||
private String realName;
|
||||
|
||||
// 学生ID
|
||||
private String studentID;
|
||||
|
||||
// 在校状态
|
||||
private boolean atSchool;
|
||||
}
|
||||
|
@ -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> 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;
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ public interface IUserService {
|
||||
List<User> findAll();
|
||||
|
||||
Optional<User> findUserById(int id);
|
||||
Optional<User> findUserByUsername(String username);
|
||||
User findUserByUsername(String username);
|
||||
|
||||
// 获得用户所有的权限角色
|
||||
Collection<? extends GrantedAuthority> 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);
|
||||
|
@ -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<User> findAll() {
|
||||
@ -34,8 +39,10 @@ public class UserService implements IUserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<User> findUserByUsername(String username) {
|
||||
return userRepository.findByUsername(username);
|
||||
public User findUserByUsername(String username) {
|
||||
Optional<User> 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);
|
||||
}
|
||||
|
||||
|
@ -17,8 +17,8 @@
|
||||
<form action="/login" method="post">
|
||||
<div class="card-body">
|
||||
<div class="form-group">
|
||||
<label for="username">用户名</label>
|
||||
<input type="text" class="form-control" id="username" name="username" placeholder="这里输入用户名" required>
|
||||
<label for="username">学号</label>
|
||||
<input type="text" class="form-control" id="username" name="username" placeholder="这里输入学号" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">密码</label>
|
||||
|
@ -17,8 +17,8 @@
|
||||
<form action="/register" method="post">
|
||||
<div class="card-body">
|
||||
<div class="form-group">
|
||||
<label for="username">用户名</label>
|
||||
<input type="text" class="form-control" id="username" name="username" placeholder="这里输入用户名">
|
||||
<label for="student-id">学号</label>
|
||||
<input type="text" class="form-control" id="student-id" name="student-id" placeholder="这里输入学号">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="password">密码</label>
|
||||
|
Loading…
Reference in New Issue
Block a user