- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道您可以通过包含 Principal
作为方法参数,在 Spring Controller 中轻松获取用户名,如下所示:
@GetMapping("/username")
@ResponseBody
public String currentUserName(Principal principal) {
return principal.getName();
}
但我最终想要访问 MyCustomUser
类的成员,该类是我使用 findBy
方法从存储库实例化的。我可以在 Controller 中放置一个辅助方法来进行查找并根据 principal.getName()
返回用户,但是我可以更进一步并绑定(bind)到 MyCustomUser
直接,如
@GetMapping("/stuff")
@ResponseBody
public String stuff(MyCustomUser user) {
return user.thing();
}
我正在考虑创建一个像( Ref )这样的转换器:
@Component
public class PrincipalToMyCustomUserConverter implements Converter<Principal, MyCustomUser> {
private MyCustomUserRepository userRepository;
public PrincipalToApplicationUserConverter(MyCustomUserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public MyCustomUser convert(Principal source) {
return this.userRepository.findByUsername(source.getName());
}
}
但我不知道这是否是获取存储库的适当方法,并且我不知道在注册转换器时如何传递存储库( Ref )。
最佳答案
您是对的,您建议的转换器不合适。您的转换器可以从 Principal
类型的对象转换为 MyCustomUser
类型的对象,但是,没有 *Principal*
可供转换。主体注入(inject)背后的魔力在于 Spring 实际上从 SecurityContextHolder 获取它,它不是从请求中反序列化的......尽管请求中存在的字段允许 Spring 创建主体>。如果您确实想注入(inject)MyCustomUser
,请使用ModelAttribute
。 ModelAttributes
可用于所有 Spring Controller 方法。
我通常喜欢将这样的东西保留在它自己的类中,因此我会定义一个类,将这个和其他 @ControllerAdvice 保存在一个地方,如下所示:
@ControllerAdvice
public class SomeControllerAdvice {
@Autowired
private MyCustomUserRepository myCustomUserRepository;
@ModelAttribute
public MyCustomUser getUser(Principal principal) {
return myCustomUserRepository.findByUsername(principal.getName());
}
}
以上内容足以使 MyCustomUser 可用于所有方法。我要指出的是,您可能需要在这里进行一些错误处理,例如如果主体为 null 则跳过等等,还让您的 findByUsername
方法返回一个可选,以便您可以解决空返回问题。
关于java - 将主体绑定(bind)到 MyCustomUser 类作为 Controller 中的方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55228483/
我试图将护照逻辑放入 Controller 文件中,但问题是当我将逻辑放入 Controller 中时,它告诉我“无法读取未定义的属性“主体””,但是当我将代码移至索引时,所有内容都会路由向右走 in
我正在学习 Javascript,我正在尝试创建一个简单的下拉菜单。我想要的功能的一个例子可以在谷歌主页的顶部菜单中看到,带有“更多”和“设置”下拉菜单。具体来说,当您单击关闭菜单时,菜单会消失。 我
我正在努力让 Swagger 正确呈现我的 ServiceStack 服务。 我希望看到一个 UserId 字符串作为表单参数,一个 PrivateCustomer 对象作为主体参数,但是尽管 Use
注意:由于随后的研究,这个问题已经完全重组。 我正在尝试从 Shiro 的主题 PrincipalCollection 中检索值.我在集合中添加了两个主体。 Username和 UUID .当我试图记
我们正在开发一个将 OAuth 2 用于两个用例的应用程序: 访问后端微服务(使用 client_credentials) 验证应用程序的用户(使用 authorization_code ,因此将用户
我有这段代码生成一个将 myNumber 乘以 5 的委托(delegate) ParameterExpression numParam = Expression.Parameter(typeof(i
我有一些jquery, $( document ).ready(function() { body=$(body).html; $("html").html(body); }); 这应
我创建了一个通用异常 DTO,它也扩展了 RuntimeException。通过这种方式,可以在应用程序中使用它,也可以将其用作 DTO。问题是当我将 DTO 应用于 ResponseEntity 构
在 Angular 5 HttpClient 中,我可以通过这种方式设置 HttpParams()。 const body = new HttpParams() .set('email', '
我正在从 RabbitMQ 读取数据,如下所示: connection = factory.newConnection(); ch = connection.createChannel() ; Str
如何使用不同类型的调用和响应主体来改造 PUT?我有一个错误限制。类型必须相同 and 。响应bodie可以包含int值,但call不应该,因为当我用int值初始化CallBody对象时,它已经包
原则上我想做这样的事情: #grab some value from outer source (i.e. file or list defined by another programer) set
我知道如何使用TextureRegions 创建动画并将其应用于非box2d 游戏中的对象。 但是在 libgdx 的 box2d 中,我不知道该怎么做。在CocosD2中,Sprite对象中有run
我有这段代码生成一个将 myNumber 乘以 5 的委托(delegate) ParameterExpression numParam = Expression.Parameter(typeof(i
我已经计算了花括号的数量,但无法弄清楚为什么类主体不完整。每次我试图修复类(class)时,都会把整个类(class)弄乱。问题出在代码中的最后一个类。最后一个花括号给我带来了类里面的麻烦。我正在使用
有人知道吗?我只能看到 ApplyTorque 和 SetAngularVelocity,我只想在将对象添加到模拟之前旋转对象,例如:所以我有一个 crate 倾斜靠在墙上,另一个 crate 是平的
我可以获得如何让图像出现在 box2d 主体上的简单答案吗?我尝试为图像和主体创建 x 和 y int,但是一旦主体移动,图像就会保持静态。如果您确实回答,请尽可能解释一下代码。如果您对我的完整源代码
我知道我可以通过使用 PolygonRegion 来做到这一点,但问题是我使用 scene2d.Stage 和几个 Actor 。您可能知道阶段使用 SpriteBatch 而我无法渲染 Polygo
您好,我有以下代码: function redirect(){ window.location.href='logged_out_chat.php'; } ...在我的标题和以下正文标记中:
我在 didBegin(contact:) 中触发了 SpriteKit 物理接触。我为要移出屏幕的 Dot 对象的实例抓取物理体,但是当我尝试像这样更改其位置时,没有任何反应: 第一种方法 /* I
我是一名优秀的程序员,十分优秀!