1 package org.springframework.security.oauth.examples.sparklr.config;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6
7 import org.springframework.beans.factory.annotation.Qualifier;
8 import org.springframework.context.annotation.Bean;
9 import org.springframework.context.annotation.Configuration;
10 import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
11 import org.springframework.http.MediaType;
12 import org.springframework.security.oauth.examples.sparklr.PhotoInfo;
13 import org.springframework.security.oauth.examples.sparklr.PhotoService;
14 import org.springframework.security.oauth.examples.sparklr.impl.PhotoServiceImpl;
15 import org.springframework.security.oauth.examples.sparklr.mvc.AccessConfirmationController;
16 import org.springframework.security.oauth.examples.sparklr.mvc.AdminController;
17 import org.springframework.security.oauth.examples.sparklr.mvc.PhotoController;
18 import org.springframework.security.oauth.examples.sparklr.mvc.PhotoServiceUserController;
19 import org.springframework.security.oauth.examples.sparklr.oauth.SparklrUserApprovalHandler;
20 import org.springframework.security.oauth2.provider.ClientDetailsService;
21 import org.springframework.security.oauth2.provider.approval.ApprovalStore;
22 import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
23 import org.springframework.security.oauth2.provider.token.TokenStore;
24 import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
25 import org.springframework.web.servlet.View;
26 import org.springframework.web.servlet.ViewResolver;
27 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
28 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
29 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
30 import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
31 import org.springframework.web.servlet.view.InternalResourceViewResolver;
32 import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
33
34 @Configuration
35 @EnableWebMvc
36 public class WebMvcConfig extends WebMvcConfigurerAdapter {
37
38 @Bean
39 public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
40 return new PropertySourcesPlaceholderConfigurer();
41 }
42
43 @Bean
44 public ContentNegotiatingViewResolver contentViewResolver() throws Exception {
45 ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
46 contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON);
47
48 InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
49 viewResolver.setPrefix("/WEB-INF/jsp/");
50 viewResolver.setSuffix(".jsp");
51
52 MappingJackson2JsonView defaultView = new MappingJackson2JsonView();
53 defaultView.setExtractValueFromSingleKeyModel(true);
54
55 ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver();
56 contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject());
57 contentViewResolver.setViewResolvers(Arrays.<ViewResolver> asList(viewResolver));
58 contentViewResolver.setDefaultViews(Arrays.<View> asList(defaultView));
59 return contentViewResolver;
60 }
61
62 @Bean
63 public PhotoServiceUserController photoServiceUserController(PhotoService photoService) {
64 PhotoServiceUserController photoServiceUserController = new PhotoServiceUserController();
65 return photoServiceUserController;
66 }
67
68 @Bean
69 public PhotoController photoController(PhotoService photoService) {
70 PhotoController photoController = new PhotoController();
71 photoController.setPhotoService(photoService);
72 return photoController;
73 }
74
75 @Bean
76 public AccessConfirmationController accessConfirmationController(ClientDetailsService clientDetailsService,
77 ApprovalStore approvalStore) {
78 AccessConfirmationController accessConfirmationController = new AccessConfirmationController();
79 accessConfirmationController.setClientDetailsService(clientDetailsService);
80 accessConfirmationController.setApprovalStore(approvalStore);
81 return accessConfirmationController;
82 }
83
84 @Bean
85 public PhotoServiceImpl photoServices() {
86 List<PhotoInfo> photos = new ArrayList<PhotoInfo>();
87 photos.add(createPhoto("1", "marissa"));
88 photos.add(createPhoto("2", "paul"));
89 photos.add(createPhoto("3", "marissa"));
90 photos.add(createPhoto("4", "paul"));
91 photos.add(createPhoto("5", "marissa"));
92 photos.add(createPhoto("6", "paul"));
93
94 PhotoServiceImpl photoServices = new PhotoServiceImpl();
95 photoServices.setPhotos(photos);
96 return photoServices;
97 }
98
99
100 @Bean
101 public AdminController adminController(TokenStore tokenStore,
102 @Qualifier("consumerTokenServices") ConsumerTokenServices tokenServices,
103 SparklrUserApprovalHandler userApprovalHandler) {
104 AdminController adminController = new AdminController();
105 adminController.setTokenStore(tokenStore);
106 adminController.setTokenServices(tokenServices);
107 adminController.setUserApprovalHandler(userApprovalHandler);
108 return adminController;
109 }
110
111 private PhotoInfo createPhoto(String id, String userId) {
112 PhotoInfo photo = new PhotoInfo();
113 photo.setId(id);
114 photo.setName("photo" + id + ".jpg");
115 photo.setUserId(userId);
116 photo.setResourceURL("/org/springframework/security/oauth/examples/sparklr/impl/resources/" + photo.getName());
117 return photo;
118 }
119
120 @Override
121 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
122 configurer.enable();
123 }
124 }