micronaut - Micronaut 框架 / 已登录但未经授权
问题描述
我在我的 micronaut 应用程序中遇到问题。如果我想访问'/index',我必须登录。当我登录时,“/index”页面会正确显示。此时,如果我想访问“/periods/list/my”,我会重定向到“unauthorized-target-url”
最奇怪的是访问 '/index' 还是可以的……这会不会是安全配置问题?
家庭控制器.java
@Secured(SecurityRule.IS_ANONYMOUS)
@Controller("/")
public class HomeController {
@Get("/")
@View("index")
@Secured(SecurityRule.IS_AUTHENTICATED)
Map<String, Object> index(@Nullable Principal principal) {
...
}
}
PeriodController.java
@Controller("/periods")
@Secured(SecurityRule.IS_ANONYMOUS)
public class PeriodController {
@Get("/list/my")
@View("periods/my")
@Secured(SecurityRule.IS_AUTHENTICATED)
Map<String, Object> myPeriods(@Nullable Principal principal) {
...
}
}
应用程序.yml
micronaut:
application:
name: ws
security:
enabled: true
endpoints:
login:
enabled: true
logout:
enabled: true
path: '/logout'
interceptUrlMap:
-
pattern: /public/**
httpMethod: GET
access:
- isAnonymous()
session:
enabled: true
login-success-target-url: '/'
login-failure-target-url: '/login/authFailed'
logout-target-url: '/'
unauthorized-target-url: '/unauthorized'
forbidden-target-url: '/forbidden'
session:
http:
cookie: true
header: false
日志
11:47:20.134 [nioEventLoopGroup-1-4] DEBUG i.m.h.server.netty.NettyHttpServer - Server localhost:8080 Received Request: GET /periods/list/my
11:47:20.134 [nioEventLoopGroup-1-4] DEBUG i.m.h.s.netty.RoutingInBoundHandler - Matching route GET - /periods/list/my
11:47:20.134 [nioEventLoopGroup-1-4] DEBUG i.m.h.s.netty.RoutingInBoundHandler - Matched route GET - /periods/list/my to controller class com.gvhr.controllers.PeriodController
11:47:20.135 [pool-1-thread-4] DEBUG i.m.s.t.reader.HttpHeaderTokenReader - Looking for bearer token in Authorization header
11:47:20.135 [pool-1-thread-4] DEBUG i.m.s.t.TokenAuthenticationFetcher - Unauthenticated request GET, /periods/list/my, no token found.
11:47:20.135 [pool-1-thread-4] DEBUG i.m.security.filters.SecurityFilter - Failure to authenticate request. GET /periods/list/my.
11:47:20.135 [pool-1-thread-4] DEBUG i.m.security.filters.SecurityFilter - Unauthorized request GET /periods/list/my. The rule provider io.micronaut.security.rules.SecuredAnnotationRule rejected the request.
11:47:20.149 [nioEventLoopGroup-1-5] DEBUG i.m.h.server.netty.NettyHttpServer - Server localhost:8080 Received Request: GET /unauthorized
11:47:20.149 [nioEventLoopGroup-1-5] DEBUG i.m.h.s.netty.RoutingInBoundHandler - Matching route GET - /unauthorized
11:47:20.149 [nioEventLoopGroup-1-5] DEBUG i.m.h.s.netty.RoutingInBoundHandler - Matched route GET - /unauthorized to controller class com.gvhr.controllers.HomeController
解决方案
我不知道这是否是您问题的根本原因,但我有类似的症状。cookie 的使用是不可预测的。有时,cookie 会在一条路径上工作,然后在另一条路径上失败。很随意。
我发现了问题:每个页面/路径都发送回具有不同路径的 cookie。这意味着页面“/”和“/test”每个都有一个cookie。当您重新访问页面时,浏览器会为页面“/”发送一个 cookie,但为“/test”发送两个 cookie。
修复,在您的application.yml中,确保cookiePath已设置:
micronaut:
session:
http:
cookiePath: /
这会强制整个服务器只使用一个 SESSION cookie。
当我记录更改请求时,我可能会更新它。
我还替换了(@Replaces)两个 Micronaut bean:SessionSecurityfilterRejectionHandler 和 SessionLoginHandler,以支持登录后自动重定向到原始页面。
我感觉 Micronaut 还没有专注于 HTML/用户交互 :)。
推荐阅读
- java - 尝试使用java中的变量将对象设置为true或false
- c# - 是否可以导出标签页?
- batch-file - 仅知道扩展名时如何删除 .part1.rar、part01.rar 等
- java - 通知APP服务器从GCM迁移到FCM
- c# - MODBUS 字转换
- c# - 使用 Microsoft 应用注册门户通过 Microsoft Graph 访问 SharePoint
- statistics - Labview 线性拟合替代方案?
- heroku - 我如何为heroku定义一个构建步骤
- java - proxyMode 如何改变 bean 的生命周期?
- java - 为什么 ImageButton 质量模糊或降低到最差?