gpt4 book ai didi

grails - 如何在 grails 中编写 accessDeniedHandler

转载 作者:行者123 更新时间:2023-12-01 14:37:32 25 4
gpt4 key购买 nike

我是 groovy 的新手,我已经按照以下方式在 grails 中实现了 CSRF token 。
在 resource.groovy 中添加 CSRF 过滤器

csrfFilter(CsrfFilter, new HttpSessionCsrfTokenRepository()) {
accessDeniedHandler = ref('fnAccessDeniedHandler')
requireCsrfProtectionMatcher = ref('fnRequireCsrfProtectionMatcher')
}

但我不知道如何初始化 fnAccessDeniedHandler 和 fnRequireCsrfProtectionMatcher 。
提前致谢。

最佳答案

ref 中的值必须是一个 bean( https://docs.grails.org/latest/guide/spring.html )。如果要覆盖 accessDeniedHandler 和 requireCsrfProtectionMatcher,则需要创建自定义类,并在 resources.groovy 中创建 bean。例如,要创建 bean fnAccessDeniedHandler,您将执行以下操作。

在resources.groovy中添加以下内容

fnAccessDeniedHandler(CustomAccessDeniedHandler)

并创建一个实现 AccessDeniedHandler 的类 CustomAccessDeniedHandler。
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

public static final Logger LOG
= Logger.getLogger(CustomAccessDeniedHandler.class);

@Override
public void handle(
HttpServletRequest request,
HttpServletResponse response,
AccessDeniedException exc) throws IOException, ServletException {

Authentication auth
= SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {
LOG.warn("User: " + auth.getName()
+ " attempted to access the protected URL: "
+ request.getRequestURI());
}

response.sendRedirect(request.getContextPath() + "/accessDenied");
}
}

关于grails - 如何在 grails 中编写 accessDeniedHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48307781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com