trigger: verify valid_volumes after runner restart
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package com.freedom.admin.config;
|
||||
|
||||
import com.freedom.admin.common.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
/**
|
||||
* @author zhangjiaxun
|
||||
* @date 2021/1/27 14:19
|
||||
* @description 全局异常处理
|
||||
*/
|
||||
@ControllerAdvice
|
||||
@Slf4j
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
/**
|
||||
* 指定处理Exception.class异常
|
||||
* */
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseBody
|
||||
public Result error(Exception e){
|
||||
e.printStackTrace();
|
||||
return Result.error().message("执行了全局异常");
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定处理MyException.class异常
|
||||
* */
|
||||
@ExceptionHandler(MyException.class)
|
||||
@ResponseBody
|
||||
public Result error(MyException e){
|
||||
log.error(e.getMessage());
|
||||
e.printStackTrace();
|
||||
return Result.error().code(e.getCode()).message(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.freedom.admin.config;
|
||||
|
||||
import com.freedom.admin.service.UserService;
|
||||
import com.freedom.admin.utils.JwtUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author zhangjiaxun
|
||||
* @date 2021/2/1 10:35
|
||||
* @description 继承OncePerRequestFilter以确保请求只被此过滤器执行一次
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
|
||||
|
||||
@Value("${jwt.header}")
|
||||
private String header;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
private JwtUtils jwtUtils;
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
FilterChain filterChain) throws ServletException, IOException {
|
||||
log.info("==============当前的请求路径url【{}】",request.getRequestURL());
|
||||
// token
|
||||
String token = request.getHeader(header);
|
||||
if (token !=null){
|
||||
// 获取username
|
||||
String username = jwtUtils.getUsernameFormToken(token);
|
||||
// 当username不为空且spring-security用户认证信息为空,进行认证
|
||||
if (username !=null && SecurityContextHolder.getContext().getAuthentication() ==null){
|
||||
UserDetails userDetails = userService.loadUserByUsername(username);
|
||||
if (userDetails != null) {
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails,null,userDetails.getAuthorities());
|
||||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 调用下一个过滤器,沿着过滤器链
|
||||
filterChain.doFilter(request,response);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.freedom.admin.config;
|
||||
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author zhangjiaxun
|
||||
* @date 2021/1/5 17:40
|
||||
* @description 对安全验证失败的请求进行响应处理
|
||||
*/
|
||||
@Component
|
||||
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
|
||||
|
||||
@Override
|
||||
public void onAuthenticationFailure(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
AuthenticationException e) throws IOException {
|
||||
//修改编码格式
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setContentType("application/json");
|
||||
|
||||
if (e instanceof BadCredentialsException){
|
||||
response.getWriter().write("用户名或密码错误");
|
||||
}else {
|
||||
response.getWriter().write("登录失败!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.freedom.admin.config;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.freedom.admin.common.Result;
|
||||
import com.freedom.admin.utils.JwtUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author zhangjiaxun
|
||||
* @date 2021/1/5 17:40
|
||||
* @description 对安全验证成功的请求进行响应处理
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
||||
|
||||
@Autowired
|
||||
private JwtUtils jwtUtils;
|
||||
|
||||
@Override
|
||||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
|
||||
|
||||
UserDetails userDetails = (UserDetails)authentication.getPrincipal();
|
||||
// 生成token
|
||||
String token = jwtUtils.generateToken(userDetails.getUsername());
|
||||
Result result = Result.ok().message("登录成功").token(token);
|
||||
// 修改编码格式
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("application/json");
|
||||
// 输出结果
|
||||
response.getWriter().write(JSON.toJSONString(result));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.freedom.admin.config;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author zhangjiaxun
|
||||
* @date 2021/1/27 14:25
|
||||
* @description 自定义异常
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class MyException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* 状态码
|
||||
* */
|
||||
private Integer code;
|
||||
|
||||
/**
|
||||
* 状态码
|
||||
* */
|
||||
private String message;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.freedom.admin.config;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.freedom.admin.common.Result;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author zhangjiaxun
|
||||
* @date 2021/2/1 14:03
|
||||
* @description 当未登录或者token失效访问接口时,自定义的返回结果
|
||||
*/
|
||||
@Component
|
||||
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||
|
||||
@Override
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
response.setContentType("application/json");
|
||||
response.getWriter().println(JSON.toJSONString(Result.error().message("尚未登录,或者登录过期 " + e.getMessage())));
|
||||
response.getWriter().flush();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package com.freedom.admin.config;
|
||||
|
||||
import com.freedom.admin.service.impl.UserServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
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.builders.WebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.CorsConfigurationSource;
|
||||
|
||||
/**
|
||||
* @author zhangjiaxun
|
||||
* @date 2021/1/5 17:40
|
||||
* @description
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
/**
|
||||
* 无权限拦截器
|
||||
*/
|
||||
@Autowired
|
||||
private RestAuthenticationEntryPoint restAuthenticationEntryPoint;
|
||||
|
||||
/**
|
||||
* 登录成功逻辑
|
||||
*/
|
||||
@Autowired
|
||||
private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;
|
||||
|
||||
/**
|
||||
* 登录失败逻辑
|
||||
*/
|
||||
@Autowired
|
||||
private MyAuthenticationFailureHandler myAuthenticationFailureHandler;
|
||||
|
||||
/**
|
||||
* 继承OncePerRequestFilter以确保请求只被此过滤器执行一次
|
||||
*/
|
||||
@Autowired
|
||||
private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
|
||||
|
||||
@Autowired
|
||||
UserServiceImpl userDetailsService;
|
||||
|
||||
@Override
|
||||
public void configure(WebSecurity web) throws Exception {
|
||||
// 放行静态资源
|
||||
web.ignoring().antMatchers("/login.html", "/pear.config.json",
|
||||
"/admin/css/**","/admin/images/**",
|
||||
"/component/code/**","/component/layui/**","/component/pear/**");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.cors().and()
|
||||
.csrf().disable()
|
||||
.sessionManagement()
|
||||
// 基于token,所以不需要session
|
||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||
.and()
|
||||
.httpBasic()
|
||||
.authenticationEntryPoint(restAuthenticationEntryPoint)
|
||||
.and()
|
||||
.authorizeRequests()// 验证码允许匿名访问
|
||||
.antMatchers("/email-sender/sendEmailVerificationCode","/sys-user/register")
|
||||
.anonymous()
|
||||
.and()
|
||||
.formLogin()
|
||||
.loginProcessingUrl("/login")
|
||||
.successHandler(myAuthenticationSuccessHandler)
|
||||
.failureHandler(myAuthenticationFailureHandler)
|
||||
.usernameParameter("username")
|
||||
.passwordParameter("password")
|
||||
.and()
|
||||
.logout()
|
||||
.logoutUrl("/logout")
|
||||
.and()
|
||||
.rememberMe()
|
||||
.rememberMeParameter("rememberme")
|
||||
.and()
|
||||
.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
|
||||
}
|
||||
|
||||
/**
|
||||
* 跨越配置
|
||||
*/
|
||||
@Bean
|
||||
public CorsConfigurationSource corsConfigurationSource() {
|
||||
return httpServletRequest -> {
|
||||
CorsConfiguration cfg = new CorsConfiguration();
|
||||
cfg.addAllowedHeader("*");
|
||||
cfg.addAllowedMethod("*");
|
||||
// 设置允许跨域请求的域名
|
||||
cfg.addAllowedOrigin("http://localhost:8080");
|
||||
// 允许任何请求
|
||||
cfg.addAllowedHeader("*");
|
||||
cfg.addExposedHeader("token");
|
||||
// 是否允许证书
|
||||
cfg.setAllowCredentials(true);
|
||||
//
|
||||
cfg.checkOrigin("http://localhost:8080");
|
||||
return cfg;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user