1
|
package eu.etaxonomy.cdm.remote.vaadin.service;
|
2
|
|
3
|
import javax.sql.DataSource;
|
4
|
|
5
|
import org.apache.log4j.Logger;
|
6
|
import org.hibernate.SessionFactory;
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
import org.springframework.context.ApplicationContext;
|
9
|
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
10
|
import org.springframework.security.authentication.AuthenticationManager;
|
11
|
import org.springframework.security.authentication.BadCredentialsException;
|
12
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
13
|
import org.springframework.security.core.Authentication;
|
14
|
import org.springframework.security.core.context.SecurityContext;
|
15
|
import org.springframework.security.core.context.SecurityContextHolder;
|
16
|
import org.springframework.stereotype.Component;
|
17
|
|
18
|
import com.vaadin.server.VaadinService;
|
19
|
import com.vaadin.ui.Notification;
|
20
|
import com.vaadin.ui.UI;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
23
|
|
24
|
/**
|
25
|
*
|
26
|
* This class handles the whole login procedure with the spring security layer.
|
27
|
* There are still some issues to be solved concerning session handling, see ticket
|
28
|
* {@link http://dev.e-taxonomy.eu/trac/ticket/3830}.<p>
|
29
|
*
|
30
|
*
|
31
|
* @author a.oppermann
|
32
|
*
|
33
|
*/
|
34
|
|
35
|
@Component
|
36
|
public class VaadinAuthenticationService{
|
37
|
|
38
|
@Autowired
|
39
|
private transient AuthenticationManager authenticationManager;
|
40
|
@Autowired
|
41
|
private transient ApplicationContext applicationContext;
|
42
|
|
43
|
@Autowired
|
44
|
private transient HibernateTransactionManager transactionManager;
|
45
|
|
46
|
@Autowired
|
47
|
private transient DataSource dataSource;
|
48
|
|
49
|
@Autowired
|
50
|
private transient SessionFactory sessionFactory;
|
51
|
|
52
|
private transient ConversationHolder conversationHolder;
|
53
|
|
54
|
Logger logger = Logger.getLogger(VaadinAuthenticationService.class);
|
55
|
|
56
|
private String userName;
|
57
|
|
58
|
public String getUserName() {
|
59
|
return userName;
|
60
|
}
|
61
|
|
62
|
public void setUserName(String userName) {
|
63
|
this.userName = userName;
|
64
|
}
|
65
|
|
66
|
public boolean authenticate(String user, String password){
|
67
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, password);
|
68
|
try{
|
69
|
Authentication authentication = authenticationManager.authenticate(token);
|
70
|
conversationHolder = new ConversationHolder(dataSource, sessionFactory, transactionManager);
|
71
|
conversationHolder.startTransaction();
|
72
|
SecurityContext context = SecurityContextHolder.getContext();
|
73
|
context.setAuthentication(authentication);
|
74
|
// SecurityContextHolder.setStrategyName( SecurityContextHolder.MODE_GLOBAL );
|
75
|
setUserName(user);
|
76
|
VaadinService.getCurrentRequest().getWrappedSession().setAttribute("context", context);
|
77
|
VaadinService.getCurrentRequest().getWrappedSession().setAttribute("isAuthenticated", true);
|
78
|
// logger.info("VaadinSession: "+ VaadinSession.getCurrent().getSession().getAttribute("context"));
|
79
|
return true;
|
80
|
|
81
|
}catch(BadCredentialsException e){
|
82
|
Notification.show("Bad credentials", Notification.Type.ERROR_MESSAGE);
|
83
|
}
|
84
|
|
85
|
return false;
|
86
|
}
|
87
|
|
88
|
public void logout(){
|
89
|
Boolean isAuth = (Boolean) VaadinService.getCurrentRequest().getAttribute("isAuthenticated");
|
90
|
if(isAuth != null){
|
91
|
VaadinService.getCurrentRequest().getWrappedSession().setAttribute("isAuthenticated", false);
|
92
|
}
|
93
|
UI ui = UI.getCurrent();
|
94
|
SecurityContextHolder.clearContext();
|
95
|
ui.close();
|
96
|
conversationHolder.clear();
|
97
|
conversationHolder.close();
|
98
|
conversationHolder.getSessionHolder().getSession().close();
|
99
|
// VaadinSession.getCurrent().close();
|
100
|
VaadinService.getCurrentRequest().getWrappedSession().invalidate();
|
101
|
ui.getSession().close();
|
102
|
ui.getPage().setLocation("/edit/");
|
103
|
// ui.close();
|
104
|
// ui.detach();
|
105
|
// Navigator navigator = ui.getNavigator();
|
106
|
// navigator.navigateTo("");
|
107
|
}
|
108
|
|
109
|
public boolean isAuthenticated(){
|
110
|
Boolean isAuth = (Boolean) VaadinService.getCurrentRequest().getWrappedSession().getAttribute("isAuthenticated");
|
111
|
if(isAuth == null || isAuth == false){
|
112
|
logout();
|
113
|
return false;
|
114
|
}
|
115
|
return true;
|
116
|
}
|
117
|
public ConversationHolder getConversationHolder(){
|
118
|
return conversationHolder;
|
119
|
}
|
120
|
|
121
|
}
|