3 import java
.util
.Collections
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.springframework
.security
.authentication
.AnonymousAuthenticationToken
;
9 import org
.springframework
.security
.authentication
.AuthenticationManager
;
10 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
11 import org
.springframework
.security
.core
.Authentication
;
12 import org
.springframework
.security
.core
.authority
.SimpleGrantedAuthority
;
13 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
14 import org
.springframework
.security
.core
.userdetails
.User
;
15 import org
.springframework
.security
.core
.userdetails
.UserDetails
;
17 /** Gateway for user login, can also generate the related UI. */
18 public class CmsLogin
{
19 private final static Log log
= LogFactory
.getLog(CmsLogin
.class);
20 private AuthenticationManager authenticationManager
;
21 private String systemKey
= "argeo";
23 protected void logInAsAnonymous() {
24 // TODO Better deal with anonymous authentication
26 List
<SimpleGrantedAuthority
> anonAuthorities
= Collections
27 .singletonList(new SimpleGrantedAuthority("ROLE_ANONYMOUS"));
28 UserDetails anonUser
= new User("anonymous", "", true, true, true,
29 true, anonAuthorities
);
30 AnonymousAuthenticationToken anonToken
= new AnonymousAuthenticationToken(
31 systemKey
, anonUser
, anonAuthorities
);
32 Authentication authentication
= authenticationManager
33 .authenticate(anonToken
);
34 SecurityContextHolder
.getContext()
35 .setAuthentication(authentication
);
36 } catch (Exception e
) {
37 throw new CmsException("Cannot authenticate", e
);
41 protected void logInWithPassword(String username
, char[] password
) {
42 UsernamePasswordAuthenticationToken token
= new UsernamePasswordAuthenticationToken(
43 username
, new String(password
));
44 Authentication authentication
= authenticationManager
46 SecurityContextHolder
.getContext().setAuthentication(authentication
);
47 if (log
.isDebugEnabled())
48 log
.debug("Authenticated as " + authentication
);
56 // public Control createUi(Composite parent, Node context)
57 // throws RepositoryException {
58 // Composite comp = new Composite(parent, SWT.NONE);
59 // comp.setLayout(new GridLayout(1, true));
60 // comp.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LOGIN);
65 // protected void refreshUi(Composite comp) {
66 // String username = SecurityContextHolder.getContext()
67 // .getAuthentication().getName();
68 // if (username.equals("anonymous"))
71 // for (Control child : comp.getChildren()) {
75 // Label l = new Label(comp, SWT.NONE);
76 // l.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_LOGIN);
77 // l.setData(RWT.MARKUP_ENABLED, true);
78 // l.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
79 // if (username != null) {
80 // l.setText("<b>" + username + "</b>");
81 // l.addMouseListener(new UserListener());
83 // l.setText("Log in");
84 // l.addMouseListener(new LoginListener());
90 public void setAuthenticationManager(
91 AuthenticationManager authenticationManager
) {
92 this.authenticationManager
= authenticationManager
;
95 public void setSystemKey(String systemKey
) {
96 this.systemKey
= systemKey
;
99 // private class UserListener extends MouseAdapter {
100 // private static final long serialVersionUID = -3565359775509786183L;
101 // private Control source;
102 // private Shell dialog;
105 // public void mouseDown(MouseEvent e) {
106 // source = ((Control) e.widget);
107 // if (dialog != null) {
112 // dialog = createDialog(source);
116 // @SuppressWarnings("serial")
117 // protected Shell createDialog(Control source) {
118 // Shell dialog = new Shell(source.getDisplay(), SWT.NO_TRIM
119 // | SWT.BORDER | SWT.ON_TOP);
120 // dialog.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU);
121 // dialog.setLayout(new GridLayout(1, false));
123 // final CmsSession cmsSession = (CmsSession) source.getDisplay()
124 // .getData(CmsSession.KEY);
126 // Label l = new Label(dialog, SWT.NONE);
127 // l.setData(RWT.CUSTOM_VARIANT, CMS_USER_MENU_ITEM);
128 // l.setText("Log out");
129 // GridData lData = new GridData(SWT.FILL, SWT.FILL, true, false);
130 // lData.widthHint = 120;
131 // l.setLayoutData(lData);
133 // l.addMouseListener(new MouseAdapter() {
134 // public void mouseDown(MouseEvent e) {
135 // SecurityContextHolder.getContext().setAuthentication(null);
136 // UserListener.this.dialog.close();
137 // UserListener.this.dialog.dispose();
138 // cmsSession.authChange();
144 // dialog.setLocation(source.toDisplay(
145 // source.getSize().x - dialog.getSize().x, source.getSize().y));
151 // private class LoginListener extends MouseAdapter {
152 // private static final long serialVersionUID = 677115566708451462L;
153 // private Control source;
154 // private Shell dialog;
157 // public void mouseDown(MouseEvent e) {
158 // source = ((Control) e.widget);
159 // if (dialog != null) {
164 // dialog = createDialog(source);
168 // @SuppressWarnings("serial")
169 // protected Shell createDialog(Control source) {
170 // Integer textWidth = 150;
171 // Shell dialog = new Shell(source.getDisplay(), SWT.NO_TRIM
172 // | SWT.BORDER | SWT.ON_TOP);
173 // dialog.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG);
174 // dialog.setLayout(new GridLayout(2, false));
176 // new Label(dialog, SWT.NONE).setText("Username");
177 // final Text username = new Text(dialog, SWT.BORDER);
178 // username.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_USERNAME);
179 // GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
180 // gd.widthHint = textWidth;
181 // username.setLayoutData(gd);
183 // new Label(dialog, SWT.NONE).setText("Password");
184 // final Text password = new Text(dialog, SWT.BORDER | SWT.PASSWORD);
185 // password.setData(RWT.CUSTOM_VARIANT, CMS_LOGIN_DIALOG_PASSWORD);
186 // gd = new GridData(SWT.FILL, SWT.FILL, true, false);
187 // gd.widthHint = textWidth;
188 // password.setLayoutData(gd);
192 // dialog.setLocation(source.toDisplay(
193 // source.getSize().x - dialog.getSize().x, source.getSize().y));
197 // TraverseListener tl = new TraverseListener() {
198 // public void keyTraversed(TraverseEvent e) {
199 // if (e.detail == SWT.TRAVERSE_RETURN)
200 // login(username.getText(), password.getTextChars());
203 // username.addTraverseListener(tl);
204 // password.addTraverseListener(tl);
208 // protected void login(String username, char[] password) {
209 // CmsSession cmsSession = (CmsSession) source.getDisplay().getData(
211 // logInWithPassword(username, password);
214 // refreshUi(source.getParent());
215 // cmsSession.authChange();