当前位置: 首页 > news >正文

网站发布后打不开百度站长平台怎么用

网站发布后打不开,百度站长平台怎么用,三端网站如何做,发展历程 网站建设SpringSecurity的高级用法,包括自定义loginUrl携带参数,自定义认证校验逻辑,自定义权限校验逻辑。 示例项目 https://github.com/qihaiyan/springcamp/tree/master/spring-advanced-security 一、概述 在项目实际开发过程中,Spr…

SpringSecurity的高级用法,包括自定义loginUrl携带参数,自定义认证校验逻辑,自定义权限校验逻辑。 示例项目 https://github.com/qihaiyan/springcamp/tree/master/spring-advanced-security

一、概述

在项目实际开发过程中,SpringSecurity默认的认证和权限校验逻辑不能实现很高的业务复杂度,这种情况下我们需要自定义这些逻辑,包括自定义loginUrl携带参数,自定义认证校验逻辑,自定义权限校验逻辑。

二、自定义loginUrl携带参数

SpringSecurity在跳转login页面时,虽然可以指定login的url,但是无法让url中携带动态参数,不如跳转到login?param=foo,其中foo需要根据特定条件动态变化,要实现这种效果,我们需要通过exceptionHandling指定自定义LoginUrlAuthenticationEntryPoint。

public class CustomLoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();public CustomLoginUrlAuthenticationEntryPoint(String loginFormUrl) {super(loginFormUrl);}@Overridepublic void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)throws IOException, ServletException {if (!super.isUseForward()) {String redirectUrl = this.buildRedirectUrlToLoginPage(request, response, authException);// change login urlredirectUrl = redirectUrl + "?param=test";this.redirectStrategy.sendRedirect(request, response, redirectUrl);} else {String redirectUrl = null;if (super.isForceHttps() && "http".equals(request.getScheme())) {redirectUrl = this.buildHttpsRedirectUrlForRequest(request);}if (redirectUrl != null) {this.redirectStrategy.sendRedirect(request, response, redirectUrl);} else {String loginForm = this.determineUrlToUseForThisRequest(request, response, authException);RequestDispatcher dispatcher = request.getRequestDispatcher(loginForm);dispatcher.forward(request, response);}}}
}

以上代码自定义CustomLoginUrlAuthenticationEntryPoint,在commence方法中我们可以按照业务需要实现自己的跳转逻辑,通过修改redirectUrl实现。

在SpringSecurity配置中通过exceptionHandling引用CustomLoginUrlAuthenticationEntryPoint:

exceptionHandling(customizer ->customizer.authenticationEntryPoint(new CustomLoginUrlAuthenticationEntryPoint("/login")))

三、自定义认证校验逻辑

SpringSecurity默认的认证逻辑是校验用户名密码是否合法,如果想增加其它的校验逻辑,需要实现AuthenticationProvider,然后在authenticationManager中指定我们自己实现的AuthenticationProvider。

public class CustomAuthenticationProvider extends DaoAuthenticationProvider {@Autowiredprivate CustomUserDetailsService customUserDetailsService;@PostConstructpublic void init() {this.setUserDetailsService(customUserDetailsService);}@Overrideprotected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) {super.additionalAuthenticationChecks(userDetails, authentication);HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();String username = userDetails.getUsername();// 自定义认证校验逻辑if (username.equals("need approval")) {log.info("invalid request is: {}", req);throw new AuthenticationServiceException("Your account is pending approval for access");}}
}

在上面自定义的CustomAuthenticationProvider中,通过重写additionalAuthenticationChecks方法进行自定义认证逻辑的实现。

然后在authenticationManager中指定CustomAuthenticationProvider:

@Bean
public AuthenticationManager authenticationManager() {return new ProviderManager(customAuthenticationProvider);
}

四、自定义权限校验逻辑

SpringSecurity可以通过在配置中通过requestMatchers指定较为灵活的权限校验策略,但是缺少一些动态特性,比如对 /foo/{param} 这种rest风格的带变量的url就处理不了,
这种情况我们可以通过自定义AuthorizationManager来实现,然后在requestMatchers中通过access方法来指定我们自定义的AuthorizationManager。

public class MyRequestAuthorizationManager implements AuthorizationManager<RequestAuthorizationContext> {private final SecurityExpressionHandler<RequestAuthorizationContext> expressionHandler = new DefaultHttpSecurityExpressionHandler();public MyRequestAuthorizationManager() {}// 自定义授权校验逻辑@Overridepublic AuthorizationDecision check(Supplier<Authentication> authentication, RequestAuthorizationContext context) {EvaluationContext ctx = this.expressionHandler.createEvaluationContext(authentication, context);String checkParam = Optional.ofNullable(ctx.lookupVariable("param")).map(String::valueOf).orElse(null);// the '/public' url doesn't need authenticationif (checkParam != null && (checkParam.equals("public"))) {return new AuthorizationDecision(true);}return new AuthorizationDecision(!ObjectUtils.isEmpty(authentication.get().getCredentials()));}
}

在MyRequestAuthorizationManager中通过重写check方法来实现自定义权限校验,rest风格的带变量的url中的变量,可以通过EvaluationContext的lookupVariable方法获取变量值。

指定自定义AuthorizationManager:

MyRequestAuthorizationManager myRequestAuthorizationManager = new MyRequestAuthorizationManager();
http.authorizeHttpRequests(authorize -> authorize.requestMatchers("/{param}").access(myRequestAuthorizationManager))
http://www.yidumall.com/news/36827.html

相关文章:

  • 做网站会什么seo长尾快速排名
  • 合肥建设厅网站百度精准推广
  • 装饰公司怎么做网站网站百度关键词排名软件
  • 信阳网站建设制作公司网站开发流程是什么
  • 企业网站制作模板青岛网站建设方案优化
  • 郑州建设信息网站福州网络营销推广公司
  • 个人作品网站策划书百度账号出售平台
  • 怎么设立网站赚广告费google学术搜索
  • 做简历哪个网站好sem推广软件哪家好
  • 网站设计制作报价图片欣赏今日油价最新
  • 陕西住建厅网站官网视频推广渠道有哪些
  • 北京网站建设制作案例淮北网站建设
  • 做党务工作常用到的网站网推什么意思
  • 如何搭建自己得网站广州建网站的公司
  • 徐州网站优化价格网络推广教程
  • 网站建设与管理案例...广州排名推广
  • 上海外贸网站建设百度贴吧怎么做推广
  • 网站建设需要客户提供什么seo搜索引擎优化试题及答案
  • 关于做数学 平方差公式的网站活动营销的方式有哪些
  • 做电影网站考什么外链网盘源码
  • 跑腿app开发公司seo软文推广工具
  • 学做PPT报告的网站营销推广工作内容
  • web前端只做网站么百度关键词优化系统
  • 建站公司 深圳搜索历史记录
  • 做幼儿园成长册的素材网站新闻头条今日要闻
  • 日本不良网站正能量入口没封我想注册一个网站怎么注册
  • 专业的赣州网站建设百中搜优化软件
  • wordpress+安装插件+ftp郑州网站优化
  • 做哪些网站比较好的可以营销的十大产品
  • 郴州北京网站建设市场营销教材电子版