增加Spring Security部分进行验证

*还没修好

Signed-off-by: Chenx221 <chenx221@yandex.com>
This commit is contained in:
Chenx221 2023-06-08 15:17:24 +08:00
parent 91dd33a1fe
commit 58d5eec5ca
10 changed files with 53 additions and 50 deletions

View File

@ -45,19 +45,7 @@
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- <bean id="characterEncodingFilter" class="org.springframework.boot.web.servlet.FilterRegistrationBean">-->
<!-- <property name="filter">-->
<!-- <bean class="org.springframework.web.filter.CharacterEncodingFilter">-->
<!-- <property name="encoding" value="UTF-8"/>-->
<!-- <property name="forceEncoding" value="true"/>-->
<!-- </bean>-->
<!-- </property>-->
<!-- <property name="urlPatterns">-->
<!-- <list>-->
<!-- <value>/*</value>-->
<!-- </list>-->
<!-- </property>-->
<!-- </bean>-->
<import resource="classpath:spring-security.xml"/>
</beans>

View File

@ -90,7 +90,7 @@
<div class="tab-content">
<div class="tab-pane fade show active" id="pills-login" role="tabpanel"
aria-labelledby="tab-login">
<form action="signin" method="POST">
<form action="/login" method="POST">
<h1 class="text-center mb-4" style="font-family: 'Noto Sans SC Light',serif">
用户登录</h1>
<div class="form-outline mb-4">
@ -115,8 +115,6 @@
</div>
</div>
<button type="submit" class="btn btn-primary btn-block mb-4">登录</button>
<%-- 显示错误信息 --%>
<!-- Register buttons -->
<div class="text-center" style="margin-bottom: 25px;">
<p>还没有账号? <a href="#!">注册一个</a></p>
</div>

View File

@ -28,9 +28,17 @@
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

View File

@ -18,26 +18,31 @@ public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping(path = "/signin", method = {RequestMethod.GET, RequestMethod.POST}) //登录
public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model) throws NoSuchAlgorithmException {
User user = userMapper.getUserByUsername(username);
Password pw = new Password(password);
// System.out.println("username:" + username + " try to login with password: " + password + " encrypted-password:"+pw.getEncryptedPassword());
if (user != null && user.getPassword().equals(pw.getEncryptedPassword())) {
model.addAttribute("message", "登陆成功");
return "redirect:/dashboard"; // 重定向到 dashboard 页面
} else {
model.addAttribute("errorMessage", "用户名或密码不正确,请重新输入。");
return "login";
}
// @RequestMapping(path = "/signin", method = {RequestMethod.GET, RequestMethod.POST}) //登录
// public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model) throws NoSuchAlgorithmException {
// User user = userMapper.getUserByUsername(username);
// Password pw = new Password(password);
//// System.out.println("username:" + username + " try to login with password: " + password + " encrypted-password:"+pw.getEncryptedPassword());
// if (user != null && user.getPassword().equals(pw.getEncryptedPassword())) {
// model.addAttribute("message", "登陆成功");
// return "redirect:/dashboard"; // 重定向到 dashboard 页面
// } else {
// model.addAttribute("errorMessage", "用户名或密码不正确,请重新输入。");
// return "login";
// }
// }
@RequestMapping(path = "/login", method = {RequestMethod.GET, RequestMethod.POST}) //登录
public String login() {
return "login";
}
@RequestMapping(path = "/signup", method = {RequestMethod.GET, RequestMethod.POST}) //注册
public String register(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("re-password") String repassword, Model model) throws NoSuchAlgorithmException {
//debug start
// System.out.println("username:" + username);
//debug end
model.addAttribute("comeFrom","reg");
model.addAttribute("comeFrom", "reg");
//检查两次输入的密码是否相同
if (!password.equals(repassword)) {
model.addAttribute("errorMessage", "两次输入的密码不一致,请重新输入。");
@ -72,11 +77,11 @@ public class UserController {
return "login";
}
// start to register
Password pw=new Password(password);
Password pw = new Password(password);
System.out.println("username:" + username + " password:" + pw.getEncryptedPassword()); //debug
user = new User(username, pw.getEncryptedPassword());
userMapper.insertUser(user);
model.addAttribute("comeFrom","log");
model.addAttribute("comeFrom", "log");
model.addAttribute("successMessage", "注册成功");
return "login";
}

View File

@ -1,13 +1,23 @@
package cyou.chenx221.pojo;
import java.util.List;
public class User {
private Integer id;
private String username;
private String password;
private String roles;
public User() {
}
public String getRoles() {
return roles;
}
public void setRoles(String roles) {
this.roles = roles;
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;

View File

@ -45,19 +45,7 @@
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- <bean id="characterEncodingFilter" class="org.springframework.boot.web.servlet.FilterRegistrationBean">-->
<!-- <property name="filter">-->
<!-- <bean class="org.springframework.web.filter.CharacterEncodingFilter">-->
<!-- <property name="encoding" value="UTF-8"/>-->
<!-- <property name="forceEncoding" value="true"/>-->
<!-- </bean>-->
<!-- </property>-->
<!-- <property name="urlPatterns">-->
<!-- <list>-->
<!-- <value>/*</value>-->
<!-- </list>-->
<!-- </property>-->
<!-- </bean>-->
<import resource="classpath:spring-security.xml"/>
</beans>

View File

@ -90,7 +90,7 @@
<div class="tab-content">
<div class="tab-pane fade show active" id="pills-login" role="tabpanel"
aria-labelledby="tab-login">
<form action="signin" method="POST">
<form action="/login" method="POST">
<h1 class="text-center mb-4" style="font-family: 'Noto Sans SC Light',serif">
用户登录</h1>
<div class="form-outline mb-4">
@ -115,8 +115,6 @@
</div>
</div>
<button type="submit" class="btn btn-primary btn-block mb-4">登录</button>
<%-- 显示错误信息 --%>
<!-- Register buttons -->
<div class="text-center" style="margin-bottom: 25px;">
<p>还没有账号? <a href="#!">注册一个</a></p>
</div>

View File

@ -28,9 +28,17 @@
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>