Simple authentication example with level without hibernate and spring

1

I would like a simple example of user authentication.

I already have the table in the database, which contains the user data and the level (Administrator, common user).

As I'm running out of time to tinker with Hibernate and Spring, I want to know if I have to put a method (I'm using the DAO standard) that does query the database by selecting username and password and if I have to create a special ManagedBean for the authentication part.

I have to create a .xhtml file for the login page.

    
asked by anonymous 21.05.2014 / 04:59

2 answers

2

The simplest way is with Filter. With the filter, which is already java, you can intercept the request and validate whether a particular user is active or not.

With the filter you can determine which folder / file the user can have logged in or not.

Here's an example: link

So you could set up a filter in web.xml:

<filter>
    <filter-name>AdminPagesFilter</filter-name>
    <filter-class>com.filter.AdminPagesFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>AdminPagesFilter</filter-name>
    <url-pattern>/pages/protected/admin/*</url-pattern>
</filter-mapping>

And a filter could be declared as:

public class AdminPagesFilter extends AbstractFilter implements Filter {

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,     FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        User user = (User) req.getSession(true).getAttribute("user");

        if (!user.isAdmin()) {
            accessDenied(request, response, req);
            return;
        }

        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }
}
    
23.05.2014 / 05:23
0

For convenience, I recommend that you use spring-security. It controls access to the pages you set and still has a Roles engine, which allows you to have different levels of access.

Here is an example of a basic login:

link

    
22.05.2014 / 15:48