Java 西罗+;Stormpath在servlet中获取当前用户

Java 西罗+;Stormpath在servlet中获取当前用户,java,servlets,shiro,stormpath,Java,Servlets,Shiro,Stormpath,我刚刚开始使用ApacheShiro和Stormpath。在jsp中,一切都正常工作,正如预期的那样。但是如何在servlet中获取当前用户数据和他的自定义字段呢 @WebServlet("/test") public class Foo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletExce

我刚刚开始使用ApacheShiro和Stormpath。在jsp中,一切都正常工作,正如预期的那样。但是如何在servlet中获取当前用户数据和他的自定义字段呢

@WebServlet("/test")
public class Foo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();

        // how to get username and custom fields hereg??
    }
}

您可以通过以下方式获取当前
主题的所有可用用户数据:

Map<String, String> userAttributes = SecurityUtils.getSubject().getPrincipals().oneByType(java.util.Map.class);
System.out.println("Account href: " + userAttributes.get("href"));
System.out.println("Username: " + userAttributes.get("username"));
// other attributes available

如果使用新的
stormpath servlet插件
,只需执行以下操作:

Account account = AccountResolver.INSTANCE.getAccount(request);
通常,您会在
HttpServlet.doGet(…)
doPost(…)
方法中调用它

有关更多详细信息,请参阅

但是,如果您使用Shiro作为最新的
stormpath Shiro核心
is
0.6.0
,那么当前(2015年10月5日)会有点混乱,这取决于
stormpath sdk api
版本,该版本与任何可用的
stormpath servlet插件所需的版本都不兼容

这可能会在他们发布插件的最终非RC版本后得到解决

如果你看了这篇博文和文章,你可能会觉得没有Shiro你也可以

Account account = AccountResolver.INSTANCE.getAccount(request);