Rev 2009 | Rev 2049 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2009 | Rev 2018 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | package pt.estgp.es.exemplos.hibernate.web; |
1 | package pt.estgp.es.exemplos.hibernate.web; |
2 | 2 | ||
- | 3 | import org.apache.log4j.Logger; |
|
3 | import pt.estgp.es.exemplos.hibernate.HibernateUtils; |
4 | import pt.estgp.es.exemplos.hibernate.HibernateUtils; |
4 | import pt.estgp.es.exemplos.hibernate.UserSession; |
5 | import pt.estgp.es.exemplos.hibernate.UserSession; |
5 | import pt.estgp.es.exemplos.hibernate.UserSessionImpl; |
6 | import pt.estgp.es.exemplos.hibernate.web.pluginSession.PluginFactory; |
- | 7 | import pt.estgp.es.exemplos.hibernate.web.pluginSession.RegisterSession; |
|
6 | 8 | ||
7 | import javax.servlet.*; |
9 | import javax.servlet.*; |
8 | import javax.servlet.http.HttpServletRequest; |
10 | import javax.servlet.http.HttpServletRequest; |
9 | import javax.servlet.http.HttpServletResponse; |
11 | import javax.servlet.http.HttpServletResponse; |
10 | import java.io.IOException; |
12 | import java.io.IOException; |
11 | 13 | ||
12 | import static org.hibernate.criterion.Restrictions.like; |
- | |
13 | - | ||
14 | public class SecurityFilter implements 1.5.0/docs/api/java/util/logging/Filter.html">Filter { |
14 | public class SecurityFilter implements 1.5.0/docs/api/java/util/logging/Filter.html">Filter { |
- | 15 | ||
- | 16 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(SecurityFilter.class); |
|
- | 17 | private static final 1.5.0/docs/api/java/lang/String.html">String SESSION_IN_REQUEST_KEY = "UserSession"; |
|
- | 18 | ||
15 | @1.5.0/docs/api/java/lang/Override.html">Override |
19 | @1.5.0/docs/api/java/lang/Override.html">Override |
16 | public void init(FilterConfig filterConfig) throws ServletException{ |
20 | public void init(FilterConfig filterConfig) throws ServletException{ |
17 | 21 | ||
18 | } |
22 | } |
19 | 23 | ||
20 | @1.5.0/docs/api/java/lang/Override.html">Override |
24 | @1.5.0/docs/api/java/lang/Override.html">Override |
21 | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException { |
25 | public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException { |
22 | HttpServletResponse response=(HttpServletResponse) servletResponse; |
26 | HttpServletResponse response = (HttpServletResponse) servletResponse; |
- | 27 | loadSessionProxy(servletRequest); |
|
- | 28 | filterChain.doFilter(servletRequest, response); |
|
- | 29 | } |
|
23 | 30 | ||
24 | HttpServletRequest request = (HttpServletRequest) servletRequest; |
31 | public static UserSession loadSessionProxy(ServletRequest servletRequest) throws 1.5.0/docs/api/java/io/IOException.html">IOException, ServletException{ |
25 | request.getSession().getId(); |
- | |
26 | //SecurityCotextProvider.getInstance().registerSessionContext(request.getSession().getId()); |
32 | UserSession session = (UserSession) servletRequest.getAttribute(SESSION_IN_REQUEST_KEY); |
27 | HibernateUtils.getCurrentSession().beginTransaction(); |
- | |
28 | - | ||
29 | /*User user= (User)HibernateUtils.getCurrentSession() |
- | |
30 | .createCriteria(User.class) |
- | |
31 | .createAlias("user", "u") |
- | |
32 | .add(like("u.nome", "jorge")) |
- | |
33 | .add(eq("username", "jcmachado")) |
- | |
34 | .add(eq("password", "123")) |
- | |
35 | //.add(Order.asc("username")) |
- | |
36 | .uniqueResult();*/ |
- | |
37 | 33 | ||
38 | User u = null; |
- | |
39 | try { |
34 | try { |
40 | UserSession session= (UserSession) HibernateUtils.getCurrentSession().load(UserSession.class, request.getSession().getId());; |
- | |
41 | if(session==null){ |
35 | if(session == null) { |
42 | session= new UserSessionImpl(); |
36 | RegisterSession plugin; |
- | 37 | plugin = (RegisterSession) PluginFactory.getPlugin(RegisterSession.class); |
|
43 | session.setCookie(request.getSession().getId()); |
38 | session = plugin.openSession(servletRequest); |
44 | HibernateUtils.getCurrentSession().save(session); |
39 | servletRequest.setAttribute(SESSION_IN_REQUEST_KEY,session); |
45 | } |
40 | } |
46 | u= session.getUser(); |
- | |
47 | } |
- | |
48 | catch (1.5.0/docs/api/java/lang/Exception.html">Exception e){ |
- | |
49 | UserSession session = new UserSessionImpl(); |
41 | } catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException | 1.5.0/docs/api/java/lang/IllegalAccessException.html">IllegalAccessException | 1.5.0/docs/api/java/lang/InstantiationException.html">InstantiationException e) { |
50 | session.setCookie(request.getSession().getId()); |
42 | logger.error(e,e); |
51 | HibernateUtils.getCurrentSession().save(session); |
43 | return null; |
52 | - | ||
53 | } |
44 | } |
54 | HibernateUtils.getCurrentSession().getTransaction().commit(); |
- | |
55 | - | ||
56 | filterChain.doFilter(servletRequest, response); |
45 | return session; |
57 | - | ||
58 | } |
46 | } |
59 | 47 | ||
60 | @1.5.0/docs/api/java/lang/Override.html">Override |
48 | @1.5.0/docs/api/java/lang/Override.html">Override |
61 | public void destroy() { |
49 | public void destroy() { |
62 | 50 | ||
63 | } |
51 | } |
64 | - | ||
65 | } |
52 | } |