我在同一个tomcat中有两个应用程序。其中一个应用程序使用 spring security 进行身份验证。我希望方法 getRemoteUser 在登录到第一个应用程序时在第二个应用程序中返回有效用户名。
有没有简单的方法来实现这一点?你能指出我最简单的解决方案吗?
感谢您的回复
我在同一个tomcat中有两个应用程序。其中一个应用程序使用 spring security 进行身份验证。我希望方法 getRemoteUser 在登录到第一个应用程序时在第二个应用程序中返回有效用户名。
有没有简单的方法来实现这一点?你能指出我最简单的解决方案吗?
感谢您的回复
I do not believe Application 2 can access application 1's security information. Not directly anyways, perhaps using remoting, or webservices will allow you to do this. However, I do not think there is a place in the spring security framework where you can go across applications and get information though java code only.
这将返回本地当前登录的用户:
String username = SecurityContextHolder.getContext()
.getAuthentication().getName();
因此,如果appA登录到appB,那么公开此控制器将返回用户名,即 appA 中用于登录 appB 的用户名。哪个 appA 应该已经知道了,嗯:
public class UserController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest req,
HttpServletResponse res) throws Exception {
String username = SecurityContextHolder.getContext()
.getAuthentication().getName();
ModelAndView mv = new ModelAndView("jsonResponse");
mv.addObject("username", username);
return mv;
}
}