在我的控制器中,当我需要活动(登录)用户时,我正在执行以下操作来获取我的UserDetails
实现:
User activeUser = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
log.debug(activeUser.getSomeCustomField());
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我认为Spring可以在这样的情况下让生活更轻松.有没有办法将UserDetails
自动装配到控制器或方法?
例如,类似于:
public ModelAndView someRequestHandler(Principal principal) { ... }
Run Code Online (Sandbox Code Playgroud)
但是,而不是得到UsernamePasswordAuthenticationToken
,我得到了一个UserDetails
?
我正在寻找一个优雅的解决方案.有任何想法吗?
我的公司一直在评估Spring MVC,以确定我们是否应该在下一个项目中使用它.到目前为止,我喜欢我所看到的内容,现在我正在查看Spring Security模块,以确定它是否可以/应该使用.
我们的安全要求非常基本; 用户只需提供用户名和密码即可访问网站的某些部分(例如获取有关其帐户的信息); 并且网站上有一些页面(常见问题解答,支持等),应该授予匿名用户访问权限.
在我创建的原型中,我一直在Session中为经过身份验证的用户存储"LoginCredentials"对象(其中只包含用户名和密码); 例如,某些控制器检查此对象是否在会话中以获取对登录用户名的引用.我正在寻找用Spring Security取代这个本土逻辑,这将有很好的好处,可以删除任何类型的"我们如何跟踪登录用户?" 和"我们如何验证用户?" 来自我的控制器/业务代码.
似乎Spring Security提供了一个(每个线程)"上下文"对象,可以从应用程序的任何位置访问用户名/主体信息...
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Run Code Online (Sandbox Code Playgroud)
...在某种程度上,这个对象是一个(全局)单例,这似乎非常不像Spring.
我的问题是:如果这是在Spring Security中访问有关经过身份验证的用户的信息的标准方法,那么将Authentication对象注入SecurityContext的可接受方式是什么,以便在单元测试需要时可用于我的单元测试认证用户?
我是否需要在每个测试用例的初始化方法中进行连接?
protected void setUp() throws Exception {
...
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(testUser.getLogin(), testUser.getPassword()));
...
}
Run Code Online (Sandbox Code Playgroud)
这似乎过于冗长.有没有更简单的方法?
该SecurityContextHolder
物体本身似乎非常联合国春天般的...
我正在使用Spring Security 3和Spring MVC 3.05.
我想打印当前登录用户的用户名,如何在Controller中获取UserDetails?
@RequestMapping(value="/index.html", method=RequestMethod.GET)
public ModelAndView indexView(){
UserDetails user = ?
mv.addObject("username", user.getUsername());
ModelAndView mv = new ModelAndView("index");
return mv;
}
Run Code Online (Sandbox Code Playgroud) Principal principal
在spring控制器中然后将其传递给服务层而不是立即获取服务层中的principal,有什么好处SecurityContextHolder.getContext().getAuthentication().getPrincipal()
?getAuthentication()
和getPrincipal()
对象是否为空(如自定义包装器)?我在Spring Security 3.1中使用Spring 3.1.0.RELEASE.我想将我的Spring用户(即当前登录的用户)注入控制器.我想这样做而不是使用
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Run Code Online (Sandbox Code Playgroud)
因为它允许我使用JUnit更轻松地测试控制器.但是,我的当前设置有问题.我的问题是,将我的用户(每个请求)注入我的控制器的正确方法是什么?在我的应用程序上下文文件中,我有......
<bean id="userDetails" class="com.myco.eventmaven.security.SecurityHolder" factory-method="getUserDetails" scope="request">
<aop:scoped-proxy />
</bean>
Run Code Online (Sandbox Code Playgroud)
在哪里我将工厂类定义为......
public class SecurityHolder {
@Autowired
private static UserService userService;
public static MyUserDetails getUserDetails() {
final Authentication a = SecurityContextHolder.getContext().getAuthentication();
if (a == null) {
return null;
} else {
final MyUserDetails reg = (MyUserDetails) a.getPrincipal();
final int userId = reg.getId();
final MyUserDetails foundUser = userService.findUserById(userId);
return foundUser;
} // if
} // getUserDetails
}
Run Code Online (Sandbox Code Playgroud)
但工厂类反复死亡,因为"userService"无法自动装配(值始终为null).我正在寻找一种更好的方法来完成所有这些,也可以轻松地集成到我的JUnit测试中.有任何想法吗?
编辑:这是我正在寻找的JUnit测试......
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "file:src/test/resources/testApplicationContext.xml" })
public class UserEventFeedsControllerTest extends …
Run Code Online (Sandbox Code Playgroud) 我希望在登录后存储用户信息,并在每个页面上显示我的登录名和用户名(使用jsp).如何在我的jsp视图中访问会话bean,该会话bean将存储登录用户的信息?
可能重复:
使用Spring Security时,获取bean中当前用户名(即SecurityContext)信息的正确方法是什么?
spring security如何检索用户名
我使用spring ldap来验证我的网络应用程序,
<ldap-server id="sss" port="389"
url="ldap://ldap.example.com:389/ou=active,ou=employees,ou=people,o=example.com?uid?sub"/>
<authentication-manager alias="authenticationManager">
<ldap-authentication-provider
server-ref="sss"
role-prefix="ROLE_">
</ldap-authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)
它工作,但我不知道如何在登录页面填写用户名,因为我想在我的页面顶部显示它.
谢谢.
可能重复:
使用Spring Security时,获取bean中当前用户名(即SecurityContext)信息的正确方法是什么?
我正在使用spring security,我想知道如何在我的控制器中检索当前登录的用户名.我可以在某处存储与登录用户名相关联的用户ID吗?目前我从Priciple对象获取用户名
public boolean populateUserName(ModelMap model, Principal principal) {
if (principal != null) {
String name = principal.getName();
model.addAttribute("username", name);
System.out.println("userName - " + name);
return true;
}
else
{
System.out.println("principal is null");
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我在每个控制器方法中获取此Principal对象.我不知道这是否是获得它的最佳方式.例如,这是代码
@RequestMapping(value = "/secure/myitem.htm", method = RequestMethod.GET)
public ModelAndView showItem(@RequestParam("listingId") String listingId,
ModelMap model, Principal principal) {
populateUserName(model, principal);
}
Run Code Online (Sandbox Code Playgroud)