Java EE 6 中的编程身份验证

发布于 2024-09-01 16:34:02 字数 869 浏览 7 评论 0原文

是否可以在 Java EE 6 中以编程方式对用户进行身份验证?

让我解释一下更多细节:

我有一个包含 Servlet 和 hibernate 的现有 Java SE 项目;我手动管理所有身份验证和访问控制:

class Authenticator {
    int Id
    string username
}

Authenticator login(string username, string password) ;

void doListData(Authenticator auth) {
    if (isLoggedIn(auth)) listData();
    else doListError
}

void doUpdateData (Authenticator auth) {
    if (isLoggedAsAdmin(auth)) updateData() ;
    else doListError();
}

void doListError () {
    listError() ;
}

并且我在该项目中集成了 J2ee/jpa/servlet 3/... (Glassfish 3)。

我见过这样的注释:

@RolesAllowed ("viewer")
void doListdata (...) {
    istData() ;
}

@RolesAllowed("admin")
void doUpdateData (...) {
    updateData() ;
}

@PermotAll
void dolisterror () {
    listerror() ;
}

但是如何在login()中手动声明我的用户处于管理员和/或查看者角色?

is it possible to authenticate programmatically a user in Java EE 6?

Let me explain with some more details:

I've got an existing Java SE project with Servlets and hibernate; where I manage manually all the authentication and access control:

class Authenticator {
    int Id
    string username
}

Authenticator login(string username, string password) ;

void doListData(Authenticator auth) {
    if (isLoggedIn(auth)) listData();
    else doListError
}

void doUpdateData (Authenticator auth) {
    if (isLoggedAsAdmin(auth)) updateData() ;
    else doListError();
}

void doListError () {
    listError() ;
}

And Im integrating J2ee/jpa/servlet 3/... (Glassfish 3) in this project.

I've seen anotations like :

@RolesAllowed ("viewer")
void doListdata (...) {
    istData() ;
}

@RolesAllowed("admin")
void doUpdateData (...) {
    updateData() ;
}

@PermotAll
void dolisterror () {
    listerror() ;
}

but how can I manually state, in login(), that my user is in the admin and/or viewer role?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

得不到的就毁灭 2024-09-08 16:34:03

谢谢你的回答,我花了一段时间才明白,但你们都是对的,

login(java.lang.String user, java.lang.String password) 

这就是我想做的。我需要登录特定角色,而不是登录我的用户:

login("admin", "admin") ;
...

:)

Thank you for your ansers, I took a while to understand it, but you're both right,

login(java.lang.String user, java.lang.String password) 

is what I want to do. Instead of login in my users, I need to login a specific role:

login("admin", "admin") ;
...

:)

少女情怀诗 2024-09-08 16:34:02

首先确保您使用的是 Servlet 3.0/3.1。 Servlet 2.4没有登录方法

    @WebServlet(name="LoginServlet", urlPatterns={"/LoginServlet"})
    public class TutorialServlet extends HttpServlet {
      protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String user = request.getParameter("user");
        String password = request.getParameter("password");
        //TODO check is user and password not null

        try (PrintWriter out = response.getWriter();){
           request.login(user, password);
           //perhaps redirect to another page on success
        } catch (ServletException e) {
            //perhaps redirect to another page to login failure
            throw new ServletException(e);
        } 
      }
    }

First make sure you are using Servlet 3.0/3.1. Servlet 2.4 does not have the login method

    @WebServlet(name="LoginServlet", urlPatterns={"/LoginServlet"})
    public class TutorialServlet extends HttpServlet {
      protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String user = request.getParameter("user");
        String password = request.getParameter("password");
        //TODO check is user and password not null

        try (PrintWriter out = response.getWriter();){
           request.login(user, password);
           //perhaps redirect to another page on success
        } catch (ServletException e) {
            //perhaps redirect to another page to login failure
            throw new ServletException(e);
        } 
      }
    }
山川志 2024-09-08 16:34:02

Hi this is covered pretty well in the sun java ee 6 tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文