Skip to content

Commit 3b474db

Browse files
initial commit
1 parent bfa9689 commit 3b474db

15 files changed

Lines changed: 686 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>net.javatutorial.tutorials</groupId>
7+
<artifactId>GlassfishFormBasedAuthentication</artifactId>
8+
<version>1</version>
9+
<packaging>war</packaging>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>javax</groupId>
18+
<artifactId>javaee-api</artifactId>
19+
<version>7.0</version>
20+
<scope>provided</scope>
21+
</dependency>
22+
</dependencies>
23+
24+
<build>
25+
<finalName>authexample</finalName>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-war-plugin</artifactId>
30+
<version>2.3</version>
31+
<configuration>
32+
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
33+
</configuration>
34+
</plugin>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-compiler-plugin</artifactId>
38+
<version>3.1</version>
39+
<configuration>
40+
<source>1.8</source>
41+
<target>1.8</target>
42+
</configuration>
43+
</plugin>
44+
</plugins>
45+
</build>
46+
47+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package net.javatutorial.tutorials.gfauthexample.ejb;
2+
3+
import java.util.List;
4+
import java.util.logging.Level;
5+
import java.util.logging.Logger;
6+
7+
import javax.ejb.Stateless;
8+
import javax.persistence.EntityManager;
9+
import javax.persistence.PersistenceContext;
10+
import javax.persistence.TypedQuery;
11+
12+
import net.javatutorial.tutorials.gfauthexample.entity.Group;
13+
import net.javatutorial.tutorials.gfauthexample.entity.User;
14+
import net.javatutorial.tutorials.gfauthexample.utils.AuthenticationUtils;
15+
16+
17+
@Stateless
18+
public class UserEJB {
19+
20+
@PersistenceContext(unitName="tutorialsPU")
21+
private EntityManager em;
22+
23+
public User createUser(User user) {
24+
try {
25+
user.setPassword(AuthenticationUtils.encodeSHA256(user.getPassword()));
26+
} catch (Exception e) {
27+
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
28+
e.printStackTrace();
29+
}
30+
31+
Group group = new Group();
32+
group.setEmail(user.getEmail());
33+
group.setGroupname(Group.USERS_GROUP);
34+
35+
em.persist(user);
36+
em.persist(group);
37+
38+
return user;
39+
}
40+
41+
public User findUserById(String id) {
42+
TypedQuery<User> query = em.createNamedQuery("findUserById", User.class);
43+
query.setParameter("email", id);
44+
User user = null;
45+
try {
46+
user = query.getSingleResult();
47+
} catch (Exception e) {
48+
// getSingleResult throws NoResultException in case there is no user in DB
49+
// ignore exception and return NULL for user instead
50+
}
51+
return user;
52+
}
53+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.javatutorial.tutorials.gfauthexample.entity;
2+
3+
import java.io.Serializable;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.Table;
9+
10+
@Entity
11+
@Table(name="user_groups")
12+
public class Group implements Serializable {
13+
14+
private static final long serialVersionUID = 1528447384986169065L;
15+
16+
public static final String USERS_GROUP = "users";
17+
18+
@Id
19+
@Column(name="email", nullable=false, length=255)
20+
private String email;
21+
22+
@Column(name="groupname", nullable=false, length=32)
23+
private String groupname;
24+
25+
public Group() {}
26+
27+
public Group(String email, String groupname) {
28+
this.email = email;
29+
this.groupname = groupname;
30+
}
31+
32+
public String getEmail() {
33+
return email;
34+
}
35+
36+
public void setEmail(String email) {
37+
this.email = email;
38+
}
39+
40+
public String getGroupname() {
41+
return groupname;
42+
}
43+
44+
public void setGroupname(String groupname) {
45+
this.groupname = groupname;
46+
}
47+
48+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.javatutorial.tutorials.gfauthexample.entity;
2+
3+
import java.io.Serializable;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.NamedQueries;
9+
import javax.persistence.NamedQuery;
10+
import javax.persistence.Table;
11+
12+
@Entity
13+
@NamedQueries({
14+
@NamedQuery(name = "findUserById", query = "SELECT u FROM User u WHERE u.email = :email")
15+
})
16+
@Table(name="users")
17+
public class User implements Serializable {
18+
19+
private static final long serialVersionUID = -5892169641074303723L;
20+
21+
@Id
22+
@Column(name="email", nullable=false, length=255)
23+
private String email;
24+
25+
@Column(name="password", nullable=false, length=64)
26+
private String password;
27+
28+
@Column(name="name", nullable=false, length=30)
29+
private String name;
30+
31+
public User(){}
32+
33+
public User(String email, String password, String name) {
34+
this.email = email;
35+
this.password = password;
36+
this.name = name;
37+
}
38+
39+
public String getEmail() {
40+
return email;
41+
}
42+
43+
public void setEmail(String email) {
44+
this.email = email;
45+
}
46+
47+
public String getPassword() {
48+
return password;
49+
}
50+
51+
public void setPassword(String password) {
52+
this.password = password;
53+
}
54+
55+
public String getName() {
56+
return name;
57+
}
58+
59+
public void setName(String name) {
60+
this.name = name;
61+
}
62+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package net.javatutorial.tutorials.gfauthexample.managedbeans;
2+
3+
import java.io.Serializable;
4+
import java.security.Principal;
5+
import java.util.Map;
6+
import java.util.logging.Level;
7+
import java.util.logging.Logger;
8+
9+
import javax.faces.application.FacesMessage;
10+
import javax.faces.bean.ManagedBean;
11+
import javax.faces.bean.SessionScoped;
12+
import javax.faces.context.ExternalContext;
13+
import javax.faces.context.FacesContext;
14+
import javax.inject.Inject;
15+
import javax.servlet.ServletException;
16+
import javax.servlet.http.HttpServletRequest;
17+
import javax.servlet.http.HttpSession;
18+
19+
import net.javatutorial.tutorials.gfauthexample.ejb.UserEJB;
20+
import net.javatutorial.tutorials.gfauthexample.entity.User;
21+
22+
@ManagedBean
23+
@SessionScoped
24+
public class LoginView implements Serializable {
25+
26+
private static final long serialVersionUID = 3254181235309041386L;
27+
28+
private static Logger log = Logger.getLogger(LoginView.class.getName());
29+
30+
@Inject
31+
private UserEJB userEJB;
32+
33+
private String email;
34+
private String password;
35+
36+
private User user;
37+
38+
public String login() {
39+
FacesContext context = FacesContext.getCurrentInstance();
40+
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
41+
42+
try {
43+
request.login(email, password);
44+
} catch (ServletException e) {
45+
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login failed!", null));
46+
return "signin";
47+
}
48+
49+
Principal principal = request.getUserPrincipal();
50+
51+
this.user = userEJB.findUserById(principal.getName());
52+
53+
log.info("Authentication done for user: " + principal.getName());
54+
55+
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
56+
Map<String, Object> sessionMap = externalContext.getSessionMap();
57+
sessionMap.put("User", user);
58+
59+
if (request.isUserInRole("users")) {
60+
return "/user/privatepage?faces-redirect=true";
61+
} else {
62+
return "signin";
63+
}
64+
}
65+
66+
public String logout() {
67+
FacesContext context = FacesContext.getCurrentInstance();
68+
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
69+
70+
try {
71+
this.user = null;
72+
request.logout();
73+
// clear the session
74+
((HttpSession) context.getExternalContext().getSession(false)).invalidate();
75+
} catch (ServletException e) {
76+
log.log(Level.SEVERE, "Failed to logout user!", e);
77+
}
78+
79+
return "/signin?faces-redirect=true";
80+
}
81+
82+
public User getAuthenticatedUser() {
83+
return user;
84+
}
85+
86+
public String getEmail() {
87+
return email;
88+
}
89+
90+
public void setEmail(String email) {
91+
this.email = email;
92+
}
93+
94+
public String getPassword() {
95+
return password;
96+
}
97+
98+
public void setPassword(String password) {
99+
this.password = password;
100+
}
101+
}

0 commit comments

Comments
 (0)