You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VAccessControl.asciidoc 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. ---
  2. title: Access Control
  3. order: 8
  4. layout: page
  5. ---
  6. [[v-access-control]]
  7. = Access control
  8. In this tutorial we will look into access control.
  9. [[basic-access-control]]
  10. Basic access control
  11. ~~~~~~~~~~~~~~~~~~~~
  12. The application we've been building will inevitably need some
  13. administrative tools. Creation and deletion of users, for example, is
  14. generally something that we'd like to do during runtime. Let's create a
  15. simple View for creating a new user:
  16. [source,java]
  17. ....
  18. package com.vaadin.cdi.tutorial;
  19. import java.util.concurrent.atomic.AtomicLong;
  20. import javax.inject.Inject;
  21. import com.vaadin.cdi.CDIView;
  22. import com.vaadin.data.Validator;
  23. import com.vaadin.data.fieldgroup.BeanFieldGroup;
  24. import com.vaadin.data.fieldgroup.FieldGroup.CommitEvent;
  25. import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
  26. import com.vaadin.data.fieldgroup.FieldGroup.CommitHandler;
  27. import com.vaadin.navigator.View;
  28. import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
  29. import com.vaadin.ui.Button;
  30. import com.vaadin.ui.Button.ClickEvent;
  31. import com.vaadin.ui.Button.ClickListener;
  32. import com.vaadin.ui.CustomComponent;
  33. import com.vaadin.ui.Label;
  34. import com.vaadin.ui.VerticalLayout;
  35. @CDIView
  36. public class CreateUserView extends CustomComponent implements View {
  37. @Inject
  38. UserDAO userDAO;
  39. private static final AtomicLong ID_FACTORY = new AtomicLong(3);
  40. @Override
  41. public void enter(ViewChangeEvent event) {
  42. final VerticalLayout layout = new VerticalLayout();
  43. layout.setMargin(true);
  44. layout.setSpacing(true);
  45. layout.addComponent(new Label("Create new user"));
  46. final BeanFieldGroup<User> fieldGroup = new BeanFieldGroup<User>(
  47. User.class);
  48. layout.addComponent(fieldGroup.buildAndBind("firstName"));
  49. layout.addComponent(fieldGroup.buildAndBind("lastName"));
  50. layout.addComponent(fieldGroup.buildAndBind("username"));
  51. layout.addComponent(fieldGroup.buildAndBind("password"));
  52. layout.addComponent(fieldGroup.buildAndBind("email"));
  53. fieldGroup.getField("username").addValidator(new Validator() {
  54. @Override
  55. public void validate(Object value) throws InvalidValueException {
  56. String username = (String) value;
  57. if (username.isEmpty()) {
  58. throw new InvalidValueException("Username cannot be empty");
  59. }
  60. if (userDAO.getUserBy(username) != null) {
  61. throw new InvalidValueException("Username is taken");
  62. }
  63. }
  64. });
  65. fieldGroup.setItemDataSource(new User(ID_FACTORY.incrementAndGet(), "",
  66. "", "", "", "", false));
  67. final Label messageLabel = new Label();
  68. layout.addComponent(messageLabel);
  69. fieldGroup.addCommitHandler(new CommitHandler() {
  70. @Override
  71. public void preCommit(CommitEvent commitEvent) throws CommitException {
  72. }
  73. @Override
  74. public void postCommit(CommitEvent commitEvent) throws CommitException {
  75. userDAO.saveUser(fieldGroup.getItemDataSource().getBean());
  76. fieldGroup.setItemDataSource(new User(ID_FACTORY
  77. .incrementAndGet(), "", "", "", "", "", false));
  78. }
  79. });
  80. Button commitButton = new Button("Create");
  81. commitButton.addClickListener(new ClickListener() {
  82. @Override
  83. public void buttonClick(ClickEvent event) {
  84. try {
  85. fieldGroup.commit();
  86. messageLabel.setValue("User created");
  87. } catch (CommitException e) {
  88. messageLabel.setValue(e.getMessage());
  89. }
  90. }
  91. });
  92. layout.addComponent(commitButton);
  93. setCompositionRoot(layout);
  94. }
  95. }
  96. ....
  97. `CDIViewProvider` checks the Views for a specific annotation,
  98. `javax.annotation.security.RolesAllowed`. You can get access to it by
  99. adding the following dependency to your pom.xml:
  100. [source,xml]
  101. ....
  102. <dependency>
  103. <groupId>javax.annotation</groupId>
  104. <artifactId>javax.annotation-api</artifactId>
  105. <version>1.2-b01</version>
  106. </dependency>
  107. ....
  108. [source,java]
  109. ....
  110. @CDIView
  111. @RolesAllowed({ "admin" })
  112. public class CreateUserView extends CustomComponent implements View {
  113. ....
  114. To add access control to our application we'll need to have a concrete
  115. implementation of the AccessControl abstract class. Vaadin CDI comes
  116. bundled with a simple JAAS implementation, but configuring a JAAS
  117. security domain is outside the scope of this tutorial. Instead we'll opt
  118. for a simpler implementation.
  119. We'll go ahead and alter our UserInfo class to include hold roles.
  120. [source,java]
  121. ....
  122. private List<String> roles = new LinkedList<String>();
  123. public void setUser(User user) {
  124. this.user = user;
  125. roles.clear();
  126. if (user != null) {
  127. roles.add("user");
  128. if (user.isAdmin()) {
  129. roles.add("admin");
  130. }
  131. }
  132. }
  133. public List<String> getRoles() {
  134. return roles;
  135. }
  136. ....
  137. Let's extend `AccessControl` and use our freshly modified `UserInfo` in it.
  138. [source,java]
  139. ....
  140. package com.vaadin.cdi.tutorial;
  141. import javax.enterprise.inject.Alternative;
  142. import javax.inject.Inject;
  143. import com.vaadin.cdi.access.AccessControl;
  144. @Alternative
  145. public class CustomAccessControl extends AccessControl {
  146. @Inject
  147. private UserInfo userInfo;
  148. @Override
  149. public boolean isUserSignedIn() {
  150. return userInfo.getUser() != null;
  151. }
  152. @Override
  153. public boolean isUserInRole(String role) {
  154. if (isUserSignedIn()) {
  155. for (String userRole : userInfo.getRoles()) {
  156. if (role.equals(userRole)) {
  157. return true;
  158. }
  159. }
  160. }
  161. return false;
  162. }
  163. @Override
  164. public String getPrincipalName() {
  165. if (isUserSignedIn()) {
  166. return userInfo.getUser().getUsername();
  167. }
  168. return null;
  169. }
  170. }
  171. ....
  172. Note the `@Alternative` annotation. The JAAS implementation is set as the
  173. default, and we can't have multiple default implementations. We'll have
  174. to add our custom implementation to the beans.xml:
  175. [source,xml]
  176. ....
  177. <beans>
  178. <alternatives>
  179. <class>com.vaadin.cdi.tutorial.UserGreetingImpl</class>
  180. <class>com.vaadin.cdi.tutorial.CustomAccessControl</class>
  181. </alternatives>
  182. <decorators>
  183. <class>com.vaadin.cdi.tutorial.NavigationLogDecorator</class>
  184. </decorators>
  185. </beans>
  186. ....
  187. Now let's add a button to navigate to this view.
  188. ChatView:
  189. [source,java]
  190. ....
  191. private Layout buildUserSelectionLayout() {
  192. VerticalLayout layout = new VerticalLayout();
  193. layout.setWidth("100%");
  194. layout.setMargin(true);
  195. layout.setSpacing(true);
  196. layout.addComponent(new Label("Select user to talk to:"));
  197. for (User user : userDAO.getUsers()) {
  198. if (user.equals(userInfo.getUser())) {
  199. continue;
  200. }
  201. layout.addComponent(generateUserSelectionButton(user));
  202. }
  203. layout.addComponent(new Label("Admin:"));
  204. Button createUserButton = new Button("Create user");
  205. createUserButton.addClickListener(new ClickListener() {
  206. @Override
  207. public void buttonClick(ClickEvent event) {
  208. navigationEvent.fire(new NavigationEvent("create-user"));
  209. }
  210. });
  211. layout.addComponent(createUserButton);
  212. return layout;
  213. }
  214. ....
  215. Everything seems to work fine, the admin is able to use this new feature
  216. to create a new user and the view is inaccessible to non-admins. An
  217. attempt to access the view without the proper authorization will
  218. currently cause an `IllegalArgumentException`. A better approach would be
  219. to create an error view and display that instead.
  220. [source,java]
  221. ....
  222. package com.vaadin.cdi.tutorial;
  223. import javax.inject.Inject;
  224. import com.vaadin.cdi.access.AccessControl;
  225. import com.vaadin.navigator.View;
  226. import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
  227. import com.vaadin.ui.Button;
  228. import com.vaadin.ui.Button.ClickEvent;
  229. import com.vaadin.ui.Button.ClickListener;
  230. import com.vaadin.ui.CustomComponent;
  231. import com.vaadin.ui.Label;
  232. import com.vaadin.ui.VerticalLayout;
  233. public class ErrorView extends CustomComponent implements View {
  234. @Inject
  235. private AccessControl accessControl;
  236. @Inject
  237. private javax.enterprise.event.Event<NavigationEvent> navigationEvent;
  238. @Override
  239. public void enter(ViewChangeEvent event) {
  240. VerticalLayout layout = new VerticalLayout();
  241. layout.setSizeFull();
  242. layout.setMargin(true);
  243. layout.setSpacing(true);
  244. layout.addComponent(new Label(
  245. "Unfortunately, the page you've requested does not exists."));
  246. if (accessControl.isUserSignedIn()) {
  247. layout.addComponent(createChatButton());
  248. } else {
  249. layout.addComponent(createLoginButton());
  250. }
  251. setCompositionRoot(layout);
  252. }
  253. private Button createLoginButton() {
  254. Button button = new Button("To login page");
  255. button.addClickListener(new ClickListener() {
  256. @Override
  257. public void buttonClick(ClickEvent event) {
  258. navigationEvent.fire(new NavigationEvent("login"));
  259. }
  260. });
  261. return button;
  262. }
  263. private Button createChatButton() {
  264. Button button = new Button("Back to the main page");
  265. button.addClickListener(new ClickListener() {
  266. @Override
  267. public void buttonClick(ClickEvent event) {
  268. navigationEvent.fire(new NavigationEvent("chat"));
  269. }
  270. });
  271. return button;
  272. }
  273. }
  274. ....
  275. To use this we'll modify our `NavigationService` to add the error view to
  276. the `Navigator`.
  277. NavigationServiceImpl:
  278. [source,java]
  279. ....
  280. @Inject
  281. private ErrorView errorView;
  282. @PostConstruct
  283. public void initialize() {
  284. if (ui.getNavigator() == null) {
  285. Navigator navigator = new Navigator(ui, ui);
  286. navigator.addProvider(viewProvider);
  287. navigator.setErrorView(errorView);
  288. }
  289. }
  290. ....
  291. We don't really want the admin-only buttons to be visible to non-admin
  292. users. To programmatically hide them we can inject `AccessControl` to our
  293. view.
  294. ChatView:
  295. [source,java]
  296. ....
  297. @Inject
  298. private AccessControl accessControl;
  299. private Layout buildUserSelectionLayout() {
  300. VerticalLayout layout = new VerticalLayout();
  301. layout.setWidth("100%");
  302. layout.setMargin(true);
  303. layout.setSpacing(true);
  304. layout.addComponent(new Label("Select user to talk to:"));
  305. for (User user : userDAO.getUsers()) {
  306. if (user.equals(userInfo.getUser())) {
  307. continue;
  308. }
  309. layout.addComponent(generateUserSelectionButton(user));
  310. }
  311. if(accessControl.isUserInRole("admin")) {
  312. layout.addComponent(new Label("Admin:"));
  313. Button createUserButton = new Button("Create user");
  314. createUserButton.addClickListener(new ClickListener() {
  315. @Override
  316. public void buttonClick(ClickEvent event) {
  317. navigationEvent.fire(new NavigationEvent("create-user"));
  318. }
  319. });
  320. layout.addComponent(createUserButton);
  321. }
  322. return layout;
  323. }
  324. ....
  325. [[some-further-topics]]
  326. Some further topics
  327. ~~~~~~~~~~~~~~~~~~~
  328. In the previous section we pruned the layout programmatically to prevent
  329. non-admins from even seeing the admin buttons. That was one way to do
  330. it. Another would be to create a custom component representing the
  331. layout, then create a producer for that component which would determine
  332. at runtime which version to create.
  333. Sometimes there's a need for a more complex custom access control
  334. implementations. You may need to use something more than Java Strings to
  335. indicate user roles, you may want to alter access rights during runtime.
  336. For those purposes we could extend the `CDIViewProvider` (with either the
  337. `@Specializes` annotation or `@Alternative` with a beans.xml entry) and
  338. override `isUserHavingAccessToView(Bean<?> viewBean)`.