Skip to content

Commit

Permalink
JSON传参+自定义Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronlinv committed Aug 22, 2021
1 parent fe65f3e commit d2badb8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
@@ -1,16 +1,25 @@
package com.al._01springsecuritydemo01.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

@EnableWebSecurity
// @Configuration 被包括在上面的注解了
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationSuccessHandler successHandler;

@Autowired
private AuthenticationFailureHandler failureHandler;

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
Expand All @@ -21,8 +30,10 @@ protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/index")
.defaultSuccessUrl("/index")
// .defaultSuccessUrl("/index")
// .defaultSuccessUrl("/index")
.successHandler(successHandler)
.failureHandler(failureHandler)

.and()
.authorizeRequests()
Expand Down
@@ -0,0 +1,25 @@
package com.al._01springsecuritydemo01.handler;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
response.setContentType("application/json; charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.write("{\"code\":\"40001\",\"msg\":\"登录失败\"}");
writer.flush();
writer.close();
}
}
@@ -0,0 +1,24 @@
package com.al._01springsecuritydemo01.handler;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
response.setContentType("application/json; charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.write("{\"code\":\"20001\",\"msg\":\"登录成功\"}");
writer.flush();
writer.close();
}
}
22 changes: 21 additions & 1 deletion src/main/resources/static/login.html
Expand Up @@ -3,13 +3,33 @@
<head>
<meta charset="UTF-8">
<title>登录</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>

<form action="/login" method="post">
<label for="username">账户</label><input type="text" name="username" id="username"><br>
<label for="password">密码</label><input type="password" name="password" id="password"><br>
<input type="submit" value="登录">
<input type="submit" onclick="login()" value="登录">
</form>
</body>
<script>
function login() {
$.ajax({
type: "POST",
url: "/login",
data: {
"username": $("#username").val(),
"password": $("#password").val(),
},
success: function (data) {
if (data.code == 20001) {
Location.href = "/index";
} else {
alert(data.msg);
}
}
})
}
</script>
</html>

0 comments on commit d2badb8

Please sign in to comment.