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

门户网站标题居中加大后端开发技术

门户网站标题居中加大,后端开发技术,手机网站加百度地图,网站备案幕布SpringSecurity的高级用法#xff0c;包括自定义loginUrl携带参数#xff0c;自定义认证校验逻辑#xff0c;自定义权限校验逻辑。 示例项目 https://github.com/qihaiyan/springcamp/tree/master/spring-advanced-security 一、概述 在项目实际开发过程中#xff0c;Spr…SpringSecurity的高级用法包括自定义loginUrl携带参数自定义认证校验逻辑自定义权限校验逻辑。 示例项目 https://github.com/qihaiyan/springcamp/tree/master/spring-advanced-security 一、概述 在项目实际开发过程中SpringSecurity默认的认证和权限校验逻辑不能实现很高的业务复杂度这种情况下我们需要自定义这些逻辑包括自定义loginUrl携带参数自定义认证校验逻辑自定义权限校验逻辑。 二、自定义loginUrl携带参数 SpringSecurity在跳转login页面时虽然可以指定login的url但是无法让url中携带动态参数不如跳转到login?paramfoo其中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 ?paramtest;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 AuthorizationManagerRequestAuthorizationContext {private final SecurityExpressionHandlerRequestAuthorizationContext expressionHandler new DefaultHttpSecurityExpressionHandler();public MyRequestAuthorizationManager() {}// 自定义授权校验逻辑Overridepublic AuthorizationDecision check(SupplierAuthentication 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 doesnt 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.hkea.cn/news/14400405/

相关文章:

  • 网站界面设计pswordpress 页面 父级
  • 网站架构发展历程的思考和心得体会心理咨询网站开发
  • 做公益的网站有哪些百度上如何创建自己的网站
  • 镇江论坛网站建设APP网站怎么做
  • 交流网站建设项目背景网站建设有什么注意
  • o2o网站建设怎么样移动端网站推广
  • 建网站价格多少网站建设需要怎么做
  • 抓取资源的网站怎么做美食电子商务网站建设规划书
  • 欧美网站设计风格企业网站建设上市公司
  • 建设大型网站设计公司做免费网站推广开头语
  • 电子商务网站对比分析中天建设集团有限公司是央企吗
  • 企业网站内容运营方案案例做注册会员和购物的网站需要什么
  • 南宁网站建设代理龙口网站建设联系电话
  • 夫唯seo培训系统优化开关在哪里
  • 判断网站到期深圳市住建局网站
  • 怎么用2级目录做网站thinkphp官方网站
  • 营销型网站的建设重点是什么自己做网站怎么做的
  • 榆林网站建设价格创建网络公司
  • 汕尾旅游攻略app跳转网站企业管理十大系统
  • 系统之家网站怎么做的网站降权不收录
  • 音乐网站建设需求分析网络加盟
  • 物流网站的建设论文一万字上海包装设计公司排行
  • 电商平台规则网站建设专家推荐乐云seo
  • 易讯企业建站系统wordpress框架
  • 神马网站快速排名软件可以在线做试卷的网站
  • win7本地做网站小网站关键词搜什么
  • 帮人负责做网站叫什么工作什么企业做网站比较好
  • 腾讯域名怎么建设网站电影采集网站流量
  • 做轻时尚的网站豪华跑车网站建设
  • 案例学 网页设计与网站建设网站建设万首先金手指12