Revision 92a4f98e
Added by Andreas Kohlbecker almost 7 years ago
src/main/java/eu/etaxonomy/cdm/addon/config/CdmVaadinConfiguration.java | ||
---|---|---|
17 | 17 |
import com.vaadin.spring.annotation.EnableVaadin; |
18 | 18 |
import com.vaadin.spring.server.SpringVaadinServlet; |
19 | 19 |
|
20 |
import eu.etaxonomy.cdm.vaadin.security.annotation.EnableAnnotationBasedAccessControl; |
|
20 | 21 |
import eu.etaxonomy.vaadin.ui.annotation.EnableVaadinSpringNavigation; |
21 | 22 |
|
22 | 23 |
/** |
... | ... | |
33 | 34 |
}) |
34 | 35 |
@EnableVaadin // this imports VaadinConfiguration |
35 | 36 |
@EnableVaadinSpringNavigation // activate the NavigationManagerBean |
37 |
@EnableAnnotationBasedAccessControl // enable annotation based per view access control |
|
36 | 38 |
public class CdmVaadinConfiguration { |
37 | 39 |
|
38 | 40 |
public static final Logger logger = Logger.getLogger(CdmVaadinConfiguration.class); |
src/main/java/eu/etaxonomy/cdm/vaadin/security/AnnotationBasedAccessControlBean.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2017 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.vaadin.security; |
|
10 |
|
|
11 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
12 |
|
|
13 |
import com.vaadin.navigator.View; |
|
14 |
import com.vaadin.spring.access.ViewInstanceAccessControl; |
|
15 |
import com.vaadin.ui.UI; |
|
16 |
|
|
17 |
import eu.etaxonomy.cdm.vaadin.security.annotation.RequireAuthentication; |
|
18 |
|
|
19 |
/** |
|
20 |
* @author a.kohlbecker |
|
21 |
* @since Apr 24, 2017 |
|
22 |
* |
|
23 |
*/ |
|
24 |
public class AnnotationBasedAccessControlBean implements ViewInstanceAccessControl { |
|
25 |
|
|
26 |
/** |
|
27 |
* {@inheritDoc} |
|
28 |
*/ |
|
29 |
@Override |
|
30 |
public boolean isAccessGranted(UI ui, String beanName, View view) { |
|
31 |
if(view.getClass().getAnnotation(RequireAuthentication.class) != null){ |
|
32 |
return SecurityContextHolder.getContext().getAuthentication().isAuthenticated(); |
|
33 |
} |
|
34 |
// no RequireAuthentication annotation => grant access |
|
35 |
return true; |
|
36 |
} |
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
} |
src/main/java/eu/etaxonomy/cdm/vaadin/security/AnnotationBasedAccessControlConfiguration.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2017 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.vaadin.security; |
|
10 |
|
|
11 |
import org.springframework.context.annotation.Bean; |
|
12 |
import org.springframework.context.annotation.Configuration; |
|
13 |
|
|
14 |
import com.vaadin.spring.annotation.UIScope; |
|
15 |
|
|
16 |
/** |
|
17 |
* @author a.kohlbecker |
|
18 |
* @since Apr 24, 2017 |
|
19 |
* |
|
20 |
*/ |
|
21 |
@Configuration |
|
22 |
public class AnnotationBasedAccessControlConfiguration { |
|
23 |
|
|
24 |
@Bean |
|
25 |
@UIScope |
|
26 |
public AnnotationBasedAccessControlBean annotationBasedAccessControlBean() { |
|
27 |
return new AnnotationBasedAccessControlBean(); |
|
28 |
} |
|
29 |
|
|
30 |
} |
src/main/java/eu/etaxonomy/cdm/vaadin/security/annotation/EnableAnnotationBasedAccessControl.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2017 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.vaadin.security.annotation; |
|
10 |
|
|
11 |
import static java.lang.annotation.ElementType.TYPE; |
|
12 |
import static java.lang.annotation.RetentionPolicy.RUNTIME; |
|
13 |
|
|
14 |
import java.lang.annotation.Documented; |
|
15 |
import java.lang.annotation.Retention; |
|
16 |
import java.lang.annotation.Target; |
|
17 |
|
|
18 |
import org.springframework.context.annotation.Import; |
|
19 |
|
|
20 |
import eu.etaxonomy.cdm.vaadin.security.AnnotationBasedAccessControlConfiguration; |
|
21 |
|
|
22 |
@Documented |
|
23 |
@Retention(RUNTIME) |
|
24 |
@Target(TYPE) |
|
25 |
@Import(AnnotationBasedAccessControlConfiguration.class) |
|
26 |
/** |
|
27 |
* @author a.kohlbecker |
|
28 |
* @since Apr 24, 2017 |
|
29 |
* |
|
30 |
*/ |
|
31 |
public @interface EnableAnnotationBasedAccessControl { |
|
32 |
|
|
33 |
} |
src/main/java/eu/etaxonomy/cdm/vaadin/security/annotation/RequireAuthentication.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2017 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.vaadin.security.annotation; |
|
10 |
|
|
11 |
import java.lang.annotation.ElementType; |
|
12 |
import java.lang.annotation.Target; |
|
13 |
|
|
14 |
/** |
|
15 |
* Annotate a Spring View with this to require users to authenticate. |
|
16 |
* |
|
17 |
* @author a.kohlbecker |
|
18 |
* @since Apr 24, 2017 |
|
19 |
* |
|
20 |
*/ |
|
21 |
|
|
22 |
@Target({ ElementType.TYPE}) |
|
23 |
public @interface RequireAuthentication { |
|
24 |
|
|
25 |
} |
src/main/java/eu/etaxonomy/cdm/vaadin/ui/AbstractAuthenticatedUI.java | ||
---|---|---|
8 | 8 |
import com.vaadin.server.VaadinRequest; |
9 | 9 |
import com.vaadin.server.VaadinServlet; |
10 | 10 |
import com.vaadin.server.VaadinSession; |
11 |
import com.vaadin.spring.navigator.SpringViewProvider; |
|
11 | 12 |
import com.vaadin.ui.UI; |
12 | 13 |
|
13 | 14 |
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinAuthentication; |
14 | 15 |
import eu.etaxonomy.cdm.vaadin.view.AuthenticationView; |
15 | 16 |
|
17 |
/** |
|
18 |
* @author c.mathew |
|
19 |
* @since 2015 |
|
20 |
* |
|
21 |
* @deprecated Use per View based authentication instead. This is provided by the {@link SpringViewProvider}. |
|
22 |
*/ |
|
23 |
@Deprecated |
|
16 | 24 |
public abstract class AbstractAuthenticatedUI extends CdmBaseUI { |
17 | 25 |
|
18 | 26 |
/** |
src/main/java/eu/etaxonomy/cdm/vaadin/view/AuthenticationPresenter.java | ||
---|---|---|
17 | 17 |
|
18 | 18 |
@Override |
19 | 19 |
public boolean login(URI uri, String context, String userName, String password) { |
20 |
|
|
20 | 21 |
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userName, password); |
21 | 22 |
AuthenticationManager authenticationManager = (AuthenticationManager) CdmSpringContextHelper.getCurrent().getBean("authenticationManager"); |
22 | 23 |
Authentication authentication = authenticationManager.authenticate(token); |
24 |
|
|
23 | 25 |
if(authentication != null && authentication.isAuthenticated()) { |
24 | 26 |
SecurityContextHolder.getContext().setAuthentication(authentication); |
25 | 27 |
CdmVaadinAuthentication cvAuthentication = (CdmVaadinAuthentication) VaadinSession.getCurrent().getAttribute(CdmVaadinAuthentication.KEY); |
... | ... | |
30 | 32 |
CdmVaadinSessionUtilities.setCurrentAttribute(CdmVaadinAuthentication.KEY, cvAuthentication); |
31 | 33 |
return true; |
32 | 34 |
} |
35 |
|
|
33 | 36 |
return false; |
34 | 37 |
} |
35 | 38 |
|
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/ListViewBean.java | ||
---|---|---|
36 | 36 |
import com.vaadin.ui.renderers.HtmlRenderer; |
37 | 37 |
|
38 | 38 |
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationItem; |
39 |
import eu.etaxonomy.cdm.vaadin.security.annotation.RequireAuthentication; |
|
39 | 40 |
import eu.etaxonomy.cdm.vaadin.util.converter.JodaDateTimeConverter; |
40 | 41 |
import eu.etaxonomy.cdm.vaadin.util.converter.UrlStringConverter; |
41 | 42 |
import eu.etaxonomy.cdm.vaadin.view.AbstractPageView; |
... | ... | |
47 | 48 |
* |
48 | 49 |
*/ |
49 | 50 |
@SpringView(name=ListViewBean.NAME) |
51 |
@RequireAuthentication |
|
50 | 52 |
public class ListViewBean extends AbstractPageView<ListPresenter> implements ListView, View { |
51 | 53 |
|
52 | 54 |
private static final long serialVersionUID = 3543300933072824713L; |
Also available in: Unified diff
Annotation based access control