forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* BAEL-509: Initial Commit - working but needs a few fixes to REST API, etc. * Fixed Authentication Failure - added subscription handlers - sufficient for Websocket Authentication/Authorization - still some issues to resolve with subscriptions and REST API * Final version * CSRF token controller - cleanup of chat wrapper
- Loading branch information
1 parent
70ae331
commit db2bb25
Showing
47 changed files
with
1,654 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
.idea | ||
target | ||
*.iml | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### Relevant Articles: | ||
- [Intro to Security and WebSockets](http://www.baeldung.com/intro-to-security-and-websockets) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
<?xml version="1.0"?> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.baeldung.springsecuredsockets</groupId> | ||
<artifactId>spring-security-mvc-socket</artifactId> | ||
<packaging>war</packaging> | ||
<version>1.0.0</version> | ||
<name>spring-security-mvc-socket</name> | ||
<properties> | ||
<springframework.version>4.3.8.RELEASE</springframework.version> | ||
<springsecurity.version>4.2.3.RELEASE</springsecurity.version> | ||
<jackson.version>2.8.7</jackson.version> | ||
<slf4j.version>1.7.25</slf4j.version> | ||
</properties> | ||
<dependencies> | ||
<!-- Spring Dependencies --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<version>${springframework.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-web</artifactId> | ||
<version>${springframework.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<version>${springframework.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<!-- Spring Security --> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-web</artifactId> | ||
<version>${springsecurity.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-config</artifactId> | ||
<version>${springsecurity.version}</version> | ||
</dependency> | ||
|
||
<!-- Data --> | ||
<dependency> | ||
<groupId>org.springframework.data</groupId> | ||
<artifactId>spring-data-jpa</artifactId> | ||
<version>1.11.3.RELEASE</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hibernate</groupId> | ||
<artifactId>hibernate-core</artifactId> | ||
<version>5.2.10.Final</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<version>1.4.196</version> | ||
</dependency> | ||
|
||
<!-- Websockets --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-websocket</artifactId> | ||
<version>${springframework.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-messaging</artifactId> | ||
<version>${springframework.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.security</groupId> | ||
<artifactId>spring-security-messaging</artifactId> | ||
<version>${springsecurity.version}</version> | ||
</dependency> | ||
|
||
<!-- Logging --> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>jcl-over-slf4j</artifactId> | ||
<version>${slf4j.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>1.2.3</version> | ||
</dependency> | ||
|
||
<!-- Servlet --> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>3.1.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet.jsp.jstl</groupId> | ||
<artifactId>jstl-api</artifactId> | ||
<version>1.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet.jsp</groupId> | ||
<artifactId>javax.servlet.jsp-api</artifactId> | ||
<version>2.3.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>jstl</artifactId> | ||
<version>1.2</version> | ||
</dependency> | ||
|
||
<!-- Jackson Dependencies --> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-core</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-annotations</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.tomcat.maven</groupId> | ||
<artifactId>tomcat7-maven-plugin</artifactId> | ||
<version>2.2</version> | ||
<configuration> | ||
<path>/spring-security-mvc-socket</path> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>3.0.0</version> | ||
<configuration> | ||
<warSourceDirectory>src/main/webapp</warSourceDirectory> | ||
<failOnMissingWebXml>false</failOnMissingWebXml> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
<finalName>spring-security-mvc-socket</finalName> | ||
</build> | ||
</project> |
56 changes: 56 additions & 0 deletions
56
...security-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/AppConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.baeldung.springsecuredsockets.config; | ||
|
||
import org.h2.tools.Server; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | ||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | ||
import org.springframework.web.servlet.resource.PathResourceResolver; | ||
import org.springframework.web.servlet.view.JstlView; | ||
import org.springframework.web.servlet.view.UrlBasedViewResolver; | ||
import java.sql.SQLException; | ||
|
||
@Configuration | ||
@EnableWebMvc | ||
@EnableJpaRepositories | ||
@ComponentScan("com.baeldung.springsecuredsockets") | ||
@Import({ SecurityConfig.class, DataStoreConfig.class, SocketBrokerConfig.class, SocketSecurityConfig.class }) | ||
public class AppConfig extends WebMvcConfigurerAdapter { | ||
|
||
public void addViewControllers(ViewControllerRegistry registry) { | ||
registry.addViewController("/").setViewName("index"); | ||
registry.addViewController("/login").setViewName("login"); | ||
registry.addViewController("/secured/socket").setViewName("socket"); | ||
registry.addViewController("/secured/success").setViewName("success"); | ||
registry.addViewController("/denied").setViewName("denied"); | ||
} | ||
|
||
@Bean | ||
public UrlBasedViewResolver viewResolver() { | ||
final UrlBasedViewResolver bean = new UrlBasedViewResolver(); | ||
bean.setPrefix("/WEB-INF/jsp/"); | ||
bean.setSuffix(".jsp"); | ||
bean.setViewClass(JstlView.class); | ||
return bean; | ||
} | ||
|
||
@Override | ||
public void addResourceHandlers(ResourceHandlerRegistry registry) { | ||
registry.addResourceHandler("/resources/**") | ||
.addResourceLocations("/", "/resources/") | ||
.setCachePeriod(3600) | ||
.resourceChain(true) | ||
.addResolver(new PathResourceResolver()); | ||
} | ||
|
||
// View H2 | ||
@Bean(initMethod="start", destroyMethod="stop") | ||
public Server h2Console () throws SQLException { | ||
return Server.createWebServer("-web","-webAllowOthers","-webDaemon","-webPort", "8082"); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...ty-mvc-socket/src/main/java/com/baeldung/springsecuredsockets/config/DataStoreConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.baeldung.springsecuredsockets.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.JpaVendorAdapter; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.orm.jpa.vendor.Database; | ||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.persistence.EntityManagerFactory; | ||
import javax.sql.DataSource; | ||
import java.util.Properties; | ||
|
||
@Configuration | ||
@ComponentScan("com.baeldung.springsecuredsockets") | ||
@EnableTransactionManagement | ||
@EnableJpaRepositories("com.baeldung.springsecuredsockets.repositories") | ||
public class DataStoreConfig { | ||
|
||
//Configuration for embededded data store through H2 | ||
@Bean | ||
public DataSource dataSource() { | ||
return new EmbeddedDatabaseBuilder() | ||
.setType(EmbeddedDatabaseType.H2) | ||
.setName("socketDB") | ||
.addScript("classpath:schema.sql") | ||
.addScript("classpath:data.sql") | ||
.setScriptEncoding("UTF-8") | ||
.continueOnError(true) | ||
.ignoreFailedDrops(true) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public JpaVendorAdapter jpaVendorAdapter() { | ||
final HibernateJpaVendorAdapter bean = new HibernateJpaVendorAdapter(); | ||
bean.setDatabase(Database.H2); | ||
bean.setGenerateDdl(true); | ||
return bean; | ||
} | ||
|
||
@Bean | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { | ||
final LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean(); | ||
bean.setDataSource(dataSource); | ||
bean.setJpaVendorAdapter(jpaVendorAdapter()); | ||
bean.setPackagesToScan("com.baeldung.springsecuredsockets"); | ||
|
||
//Set properties on Hibernate | ||
Properties properties = new Properties(); | ||
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); | ||
properties.setProperty("hibernate.hbm2ddl.auto", "update"); | ||
bean.setJpaProperties(properties); | ||
|
||
return bean; | ||
} | ||
|
||
@Bean | ||
public JpaTransactionManager transactionManager(EntityManagerFactory emf) { | ||
return new JpaTransactionManager(emf); | ||
} | ||
|
||
} |
Oops, something went wrong.