我正在尝试使用spring-social和spring-security来实现我的spring 5 MVC应用程序的Facebook登录。
在我看来,配置很好,我被重定向到Facebook进行登录,但是之后我的应用无法读取响应。
我试图通过配置MessageConverter(您可以在MVC config中找到该配置)来解决它,但是它不起作用。
我的Mvcconfig:
@Configuration
@ComponentScan(basePackages="cz.aloppe.didactilo.app")
@Import({PersistanceConfig.class, MongodbConfig.class})
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurationSupport{
@Autowired
private ApplicationContext applicationContext;
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(converter());
addDefaultHttpMessageConverters(converters);
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
Map<String,MediaType> mediaTypes = new HashMap<>();
mediaTypes.put("json", MediaType.APPLICATION_JSON);
configurer.mediaTypes(mediaTypes);
}
@Bean
public MappingJackson2HttpMessageConverter converter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "javascript", Charset.forName("UTF-8")), new MediaType("text", "html", Charset.forName("UTF-8"))));
return converter;
}
private ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver);
//Necessary for thymeleaf expressions like sec:authorized;
Set<IDialect> dialects = new HashSet<IDialect>();
dialects.add(springSecurityDialect());
engine.setAdditionalDialects(dialects);
return engine;
}
@Override
public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public SpringSecurityDialect springSecurityDialect(){
return new SpringSecurityDialect();
}
@Bean
public ViewResolver htmlViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine(htmlTemplateResolver()));
resolver.setContentType("text/html");
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
private ITemplateResolver htmlTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.HTML);
return resolver;
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
registry.addViewController("/home").setViewName("home");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/static/");
}
}
SecurityConfig
@EnableWebSecurity
@Configuration
@ComponentScan(basePackages = { "cz.aloppe.didactilo.app.service", "cz.aloppe.didactilo.app.controller" })
@PropertySource("classpath:application.properties")
@Import({PersistanceConfig.class})
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public ClientRegistrationRepository clientRegistrationRepository() {
List<ClientRegistration> registrations = new ArrayList<>();
registrations.add(CommonOAuth2Provider.FACEBOOK.getBuilder("facebook")
.clientId("REAL ID")
.clientSecret("REAL SECRET")
.scope("email")
.build());
return new InMemoryClientRegistrationRepository(registrations);
}
@Bean
public OAuth2AuthorizedClientService authorizedClientService() {
return new InMemoryOAuth2AuthorizedClientService(
clientRegistrationRepository());
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userService())
.passwordEncoder(passwordEncoder());
}
@Bean
protected CustomUserService userService(){
return new CustomUserService();
}
@Bean
protected CustomAuthenticationService authenticationService() {
return new CustomAuthenticationService();
}
@Override
protected void configure(HttpSecurity http) throws Exception{
http.authorizeRequests().anyRequest().authenticated()
.and()
.oauth2Login()
.clientRegistrationRepository(clientRegistrationRepository())
.authorizedClientService(authorizedClientService());
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
这是pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<environment>local</environment>
<appPath>local</appPath>
<srvUrl>localhost</srvUrl>
<java.version>1.8</java.version>
<spring.version>5.1.0.RELEASE</spring.version>
<spring-data.version>2.0.5.RELEASE</spring-data.version>
<spring-data.commons.version>2.0.5.RELEASE</spring-data.commons.version>
<spring-data.mongodb.version>2.0.0.RELEASE</spring-data.mongodb.version>
<cglib.version>2.2.2</cglib.version>
<junit.version>4.12</junit.version>
<mariadb.version>2.3.0</mariadb.version>
<mongodb.version>3.9.1</mongodb.version>
</properties>
<dependencies>
<!-- Basic spring library -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<!-- Hibernate like ORM, we use it for MongoJPA to access mongoDB -->
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<!-- Contains JPARepository class that enables to create queries to mariadb -->
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data.version}</version>
</dependency>
<dependency>
<!-- Enables to work with JPA repositories -->
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>${spring-data.commons.version}</version>
</dependency>
<dependency>
<!-- Contains MongoJPA (Jpa repository for mongo DB) that enables to create requests to mongoDB -->
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${spring-data.mongodb.version}</version>
</dependency>
<dependency>
<!-- Enables integration tests -->
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<!-- Enables to return index.html as response to controller call. Enables to add angular frontend to BE server -->
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<!-- Enables connection to mariaDB -->
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb.version}</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<!-- Enables connection to mongoDB (equivalent of JBDC) -->
<artifactId>mongo-java-driver</artifactId>
<version>${mongodb.version}</version>
</dependency>
<dependency>
<!-- Enables use of hibernate ORM -->
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<!-- Contains class DataSource that contains connection info for Hibernate to connect to MariaDB -->
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>7.0.55</version>
</dependency>
<dependency>
<!-- Enables use of junit tests -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- Spring security core. Contains basic functions and object for authentification and authorization -->
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<!-- Spring security core. Contains basic functions and object for authentification and authorization -->
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<!-- Adds new tags to JSP that allows to denominate user by role -->
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<!-- Thymeleaf expression engine -->
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<!-- Thymeleaf spring integration -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.5.M3</version>
</dependency>
<dependency>
<!-- Contains features like sec:authorized hasRole etc. to get info about user being authorized -->
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<!-- java EE, contains servlets -->
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<!-- Facebook login -->
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-facebook</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-config</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
</dependencies>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
谢谢您的帮助,因为现在我绝对不知道该怎么做。