Skip to content
Snippets Groups Projects
Commit e727a4b1 authored by Yulong Wang's avatar Yulong Wang
Browse files

Merge branch 'wyl' into 'main'

Added catch exception filter

See merge request !5
parents 4c5101da f1689733
No related branches found
No related tags found
1 merge request!5Added catch exception filter
Showing
with 193 additions and 68 deletions
......@@ -2,18 +2,20 @@ package com.cardiff.client_project.config;
import com.cardiff.client_project.utils.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* Customize fail filters
*/
public class MyAuthenticationFailHandler implements AuthenticationFailureHandler {
@Component
public class AuthenticationFailHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException{
// Configuration response
......
package com.cardiff.client_project.config;
import com.cardiff.client_project.constant.ResponseCode;
import com.cardiff.client_project.controller.CodeController;
import com.cardiff.client_project.utils.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import java.io.IOException;
......@@ -18,8 +15,8 @@ import java.io.IOException;
/**
* Customize success filters
*/
@Configuration
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Component
public class AuthenticationSuccessHandler implements org.springframework.security.web.authentication.AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException{
......
package com.cardiff.client_project.config;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
response.sendRedirect("/fault.html");
}
}
package com.cardiff.client_project.config;
import com.cardiff.client_project.constant.ResponseCode;
import com.cardiff.client_project.utils.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
response.sendRedirect("/fault.html");
}
}
package com.cardiff.client_project.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@Component
public class WebMvcConfiguration implements WebMvcConfigurer {
/**
* Set a static resource mapping
* @param registry
*/
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/*.html").addResourceLocations("classpath:/static/html/");
registry.addResourceHandler("/*.html")
.addResourceLocations("classpath:/static/html/")
.addResourceLocations("/*.img")
.addResourceLocations("classpath:/static/img/");
}
}
package com.cardiff.client_project.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
@Configuration
@Component
public class WebSecurityConfig{
//exception handler
@Autowired
//Unauthenticated exception filter
private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
@Autowired
//Insufficient permission exception processor
private AccessDeniedHandler customAccessDeniedHandler;
/**
* Coded filter
* @return
......@@ -23,6 +32,7 @@ public class WebSecurityConfig{
return new BCryptPasswordEncoder();
}
/**
* Set filter chain
* @param http
......@@ -38,10 +48,15 @@ public class WebSecurityConfig{
.requestMatchers("/commonUser.html").authenticated()
.anyRequest().permitAll()
)
//Custom permission exception handler
.exceptionHandling(exceptions -> exceptions
.authenticationEntryPoint(customAuthenticationEntryPoint)
.accessDeniedHandler(customAccessDeniedHandler)
)
.formLogin(form -> form
.loginPage("/login")
.successHandler(new MyAuthenticationSuccessHandler())
.failureHandler(new MyAuthenticationFailHandler())
.successHandler(new AuthenticationSuccessHandler())
.failureHandler(new AuthenticationFailHandler())
.permitAll());
return http.build();
......
package com.cardiff.client_project.controller;
import com.cardiff.client_project.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
......@@ -17,4 +18,10 @@ public class CodeController {
modelAndView.setViewName("superAdmin.html");
return modelAndView;
}
@GetMapping("/fault")
public Result faultController(Principal principal){
return Result.error();
}
}
......@@ -27,4 +27,6 @@ public class UserLogin implements UserDetailsService {
UserDetails userDetails = User.withUsername(inforByName.getName()).password(inforByName.getPassword()).authorities(authority).build();
return userDetails;
}
}
\ No newline at end of file
.login-container {
width: 300px;
padding: 20px;
background-color: #fff;
border-radius: 8px;
filter: opacity(90%);
/* box-shadow: 10px 5px 5px rgba(0, 0, 0, 0.1);*/
/*!*rgba(0, 0, 0, 0.1);*!*/
}
.login-container h2 {
text-align: center;
color: #333;
}
.login-container label {
font-size: 14px;
color: #555;
}
#username,#password{
width: 95%;
padding: 10px;
margin: 10px 0 20px;
border: 1px solid #ccc;
border-radius: 4px;
}
.login-container button {
width: 100%;
padding: 10px;
background-color: #4CAF50;
border: none;
color: white;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
}
.login-container button:hover {
background-color: #45a049;
}
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
超级管理员
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<main>
<p >If you do not have sufficient access rights, the page will be redirected to the login page within 10 seconds</p>
<p id="time"></p>
<button onclick="skip()">Immediate jump</button>
</main>
</body>
<script>
window.addEventListener("DOMContentLoaded", function() {
var text = document.getElementById('time')
console.log(text.innerText)
let i = 10;
var timer = setInterval(function() {
i--;
text.innerHTML = i;
if (i == 0) {
location.href = "login.html";
clearInterval(timer);
}
console.log(i);
}, 1000);
})
// 如果点击立即返回按钮直接返回主页面
function skip(){
window.location.href="login.html"
}
</script>
</html>
\ No newline at end of file
......@@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>-->
<link rel="stylesheet" type="text/css" href="/css/login.css" >
<title>Login Page</title>
<style>
body {
......@@ -12,69 +12,26 @@
align-items: center;
height: 100vh;
margin: 0;
background-image:url("/img/park-photo.jpg");
background-image:url("/img/R.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
.login-container {
width: 300px;
padding: 20px;
background-color: #fff;
border-radius: 8px;
/* box-shadow: 10px 5px 5px rgba(0, 0, 0, 0.1);*/
/*!*rgba(0, 0, 0, 0.1);*!*/
}
.login-container h2 {
text-align: center;
color: #333;
}
.login-container label {
font-size: 14px;
color: #555;
}
#username,#password{
width: 95%;
padding: 10px;
margin: 10px 0 20px;
border: 1px solid #ccc;
border-radius: 4px;
}
.login-container button {
width: 100%;
padding: 10px;
background-color: #4CAF50;
border: none;
color: white;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
}
.login-container button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<form id="loginForm" action="/login" method="POST">
<label for="username">Username</label>
<input type="text" name="username" id="username" required>
<form id="loginForm" action="/login" method="POST">
<label for="username">Username</label>
<input type="text" name="username" id="username" required>
<label for="password">Password</label>
<input type="password" name="password" id="password" required>
<label for="password">Password</label>
<input type="password" name="password" id="password" required>
<button type="submit">Login</button>
</form>
<button type="submit">Login</button>
</form>
</div>
</div>
<script>
const form = document.getElementById('loginForm');
form.addEventListener('submit', function (event) {
......
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
超级管理者
</body>
</html>
\ No newline at end of file
src/main/resources/static/img/R.jpg

30.5 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment