Skip to content

Commit db2bb25

Browse files
ThoughtscriptKevinGilmore
authored andcommitted
BAEL-509 (eugenp#2114)
* 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
1 parent 70ae331 commit db2bb25

47 files changed

Lines changed: 1654 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
<module>spring-security-mvc-login</module>
192192
<module>spring-security-mvc-persisted-remember-me</module>
193193
<module>spring-security-mvc-session</module>
194+
<module>spring-security-mvc-socket</module>
194195
<module>spring-security-rest-basic-auth</module>
195196
<module>spring-security-rest-custom</module>
196197
<module>spring-security-rest-full</module>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.idea
3+
target
4+
*.iml
5+
out
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Relevant Articles:
2+
- [Intro to Security and WebSockets](http://www.baeldung.com/intro-to-security-and-websockets)

‎spring-security-mvc-socket/pom.xml‎

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung.springsecuredsockets</groupId>
6+
<artifactId>spring-security-mvc-socket</artifactId>
7+
<packaging>war</packaging>
8+
<version>1.0.0</version>
9+
<name>spring-security-mvc-socket</name>
10+
<properties>
11+
<springframework.version>4.3.8.RELEASE</springframework.version>
12+
<springsecurity.version>4.2.3.RELEASE</springsecurity.version>
13+
<jackson.version>2.8.7</jackson.version>
14+
<slf4j.version>1.7.25</slf4j.version>
15+
</properties>
16+
<dependencies>
17+
<!-- Spring Dependencies -->
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-core</artifactId>
21+
<version>${springframework.version}</version>
22+
<exclusions>
23+
<exclusion>
24+
<groupId>commons-logging</groupId>
25+
<artifactId>commons-logging</artifactId>
26+
</exclusion>
27+
</exclusions>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework</groupId>
31+
<artifactId>spring-web</artifactId>
32+
<version>${springframework.version}</version>
33+
<exclusions>
34+
<exclusion>
35+
<groupId>commons-logging</groupId>
36+
<artifactId>commons-logging</artifactId>
37+
</exclusion>
38+
</exclusions>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework</groupId>
42+
<artifactId>spring-webmvc</artifactId>
43+
<version>${springframework.version}</version>
44+
<exclusions>
45+
<exclusion>
46+
<groupId>commons-logging</groupId>
47+
<artifactId>commons-logging</artifactId>
48+
</exclusion>
49+
</exclusions>
50+
</dependency>
51+
52+
<!-- Spring Security -->
53+
<dependency>
54+
<groupId>org.springframework.security</groupId>
55+
<artifactId>spring-security-web</artifactId>
56+
<version>${springsecurity.version}</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.springframework.security</groupId>
60+
<artifactId>spring-security-config</artifactId>
61+
<version>${springsecurity.version}</version>
62+
</dependency>
63+
64+
<!-- Data -->
65+
<dependency>
66+
<groupId>org.springframework.data</groupId>
67+
<artifactId>spring-data-jpa</artifactId>
68+
<version>1.11.3.RELEASE</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.hibernate</groupId>
72+
<artifactId>hibernate-core</artifactId>
73+
<version>5.2.10.Final</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.h2database</groupId>
77+
<artifactId>h2</artifactId>
78+
<version>1.4.196</version>
79+
</dependency>
80+
81+
<!-- Websockets -->
82+
<dependency>
83+
<groupId>org.springframework</groupId>
84+
<artifactId>spring-websocket</artifactId>
85+
<version>${springframework.version}</version>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.springframework</groupId>
89+
<artifactId>spring-messaging</artifactId>
90+
<version>${springframework.version}</version>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.springframework.security</groupId>
94+
<artifactId>spring-security-messaging</artifactId>
95+
<version>${springsecurity.version}</version>
96+
</dependency>
97+
98+
<!-- Logging -->
99+
<dependency>
100+
<groupId>org.slf4j</groupId>
101+
<artifactId>jcl-over-slf4j</artifactId>
102+
<version>${slf4j.version}</version>
103+
</dependency>
104+
<dependency>
105+
<groupId>ch.qos.logback</groupId>
106+
<artifactId>logback-classic</artifactId>
107+
<version>1.2.3</version>
108+
</dependency>
109+
110+
<!-- Servlet -->
111+
<dependency>
112+
<groupId>javax.servlet</groupId>
113+
<artifactId>javax.servlet-api</artifactId>
114+
<version>3.1.0</version>
115+
</dependency>
116+
<dependency>
117+
<groupId>javax.servlet.jsp.jstl</groupId>
118+
<artifactId>jstl-api</artifactId>
119+
<version>1.2</version>
120+
</dependency>
121+
<dependency>
122+
<groupId>javax.servlet.jsp</groupId>
123+
<artifactId>javax.servlet.jsp-api</artifactId>
124+
<version>2.3.1</version>
125+
</dependency>
126+
<dependency>
127+
<groupId>javax.servlet</groupId>
128+
<artifactId>jstl</artifactId>
129+
<version>1.2</version>
130+
</dependency>
131+
132+
<!-- Jackson Dependencies -->
133+
<dependency>
134+
<groupId>com.fasterxml.jackson.core</groupId>
135+
<artifactId>jackson-core</artifactId>
136+
<version>${jackson.version}</version>
137+
</dependency>
138+
<dependency>
139+
<groupId>com.fasterxml.jackson.core</groupId>
140+
<artifactId>jackson-databind</artifactId>
141+
<version>${jackson.version}</version>
142+
</dependency>
143+
<dependency>
144+
<groupId>com.fasterxml.jackson.core</groupId>
145+
<artifactId>jackson-annotations</artifactId>
146+
<version>${jackson.version}</version>
147+
</dependency>
148+
</dependencies>
149+
<build>
150+
<pluginManagement>
151+
<plugins>
152+
<plugin>
153+
<artifactId>maven-compiler-plugin</artifactId>
154+
<version>3.1</version>
155+
<configuration>
156+
<source>1.8</source>
157+
<target>1.8</target>
158+
</configuration>
159+
</plugin>
160+
<plugin>
161+
<groupId>org.apache.tomcat.maven</groupId>
162+
<artifactId>tomcat7-maven-plugin</artifactId>
163+
<version>2.2</version>
164+
<configuration>
165+
<path>/spring-security-mvc-socket</path>
166+
</configuration>
167+
</plugin>
168+
<plugin>
169+
<groupId>org.apache.maven.plugins</groupId>
170+
<artifactId>maven-war-plugin</artifactId>
171+
<version>3.0.0</version>
172+
<configuration>
173+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
174+
<failOnMissingWebXml>false</failOnMissingWebXml>
175+
</configuration>
176+
</plugin>
177+
</plugins>
178+
</pluginManagement>
179+
<finalName>spring-security-mvc-socket</finalName>
180+
</build>
181+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.springsecuredsockets.config;
2+
3+
import org.h2.tools.Server;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Import;
8+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
9+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
10+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
11+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
12+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
13+
import org.springframework.web.servlet.resource.PathResourceResolver;
14+
import org.springframework.web.servlet.view.JstlView;
15+
import org.springframework.web.servlet.view.UrlBasedViewResolver;
16+
import java.sql.SQLException;
17+
18+
@Configuration
19+
@EnableWebMvc
20+
@EnableJpaRepositories
21+
@ComponentScan("com.baeldung.springsecuredsockets")
22+
@Import({ SecurityConfig.class, DataStoreConfig.class, SocketBrokerConfig.class, SocketSecurityConfig.class })
23+
public class AppConfig extends WebMvcConfigurerAdapter {
24+
25+
public void addViewControllers(ViewControllerRegistry registry) {
26+
registry.addViewController("/").setViewName("index");
27+
registry.addViewController("/login").setViewName("login");
28+
registry.addViewController("/secured/socket").setViewName("socket");
29+
registry.addViewController("/secured/success").setViewName("success");
30+
registry.addViewController("/denied").setViewName("denied");
31+
}
32+
33+
@Bean
34+
public UrlBasedViewResolver viewResolver() {
35+
final UrlBasedViewResolver bean = new UrlBasedViewResolver();
36+
bean.setPrefix("/WEB-INF/jsp/");
37+
bean.setSuffix(".jsp");
38+
bean.setViewClass(JstlView.class);
39+
return bean;
40+
}
41+
42+
@Override
43+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
44+
registry.addResourceHandler("/resources/**")
45+
.addResourceLocations("/", "/resources/")
46+
.setCachePeriod(3600)
47+
.resourceChain(true)
48+
.addResolver(new PathResourceResolver());
49+
}
50+
51+
// View H2
52+
@Bean(initMethod="start", destroyMethod="stop")
53+
public Server h2Console () throws SQLException {
54+
return Server.createWebServer("-web","-webAllowOthers","-webDaemon","-webPort", "8082");
55+
}
56+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.springsecuredsockets.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
7+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
8+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
9+
import org.springframework.orm.jpa.JpaTransactionManager;
10+
import org.springframework.orm.jpa.JpaVendorAdapter;
11+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
12+
import org.springframework.orm.jpa.vendor.Database;
13+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
14+
import org.springframework.transaction.annotation.EnableTransactionManagement;
15+
16+
import javax.persistence.EntityManagerFactory;
17+
import javax.sql.DataSource;
18+
import java.util.Properties;
19+
20+
@Configuration
21+
@ComponentScan("com.baeldung.springsecuredsockets")
22+
@EnableTransactionManagement
23+
@EnableJpaRepositories("com.baeldung.springsecuredsockets.repositories")
24+
public class DataStoreConfig {
25+
26+
//Configuration for embededded data store through H2
27+
@Bean
28+
public DataSource dataSource() {
29+
return new EmbeddedDatabaseBuilder()
30+
.setType(EmbeddedDatabaseType.H2)
31+
.setName("socketDB")
32+
.addScript("classpath:schema.sql")
33+
.addScript("classpath:data.sql")
34+
.setScriptEncoding("UTF-8")
35+
.continueOnError(true)
36+
.ignoreFailedDrops(true)
37+
.build();
38+
}
39+
40+
@Bean
41+
public JpaVendorAdapter jpaVendorAdapter() {
42+
final HibernateJpaVendorAdapter bean = new HibernateJpaVendorAdapter();
43+
bean.setDatabase(Database.H2);
44+
bean.setGenerateDdl(true);
45+
return bean;
46+
}
47+
48+
@Bean
49+
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
50+
final LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
51+
bean.setDataSource(dataSource);
52+
bean.setJpaVendorAdapter(jpaVendorAdapter());
53+
bean.setPackagesToScan("com.baeldung.springsecuredsockets");
54+
55+
//Set properties on Hibernate
56+
Properties properties = new Properties();
57+
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
58+
properties.setProperty("hibernate.hbm2ddl.auto", "update");
59+
bean.setJpaProperties(properties);
60+
61+
return bean;
62+
}
63+
64+
@Bean
65+
public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
66+
return new JpaTransactionManager(emf);
67+
}
68+
69+
}

0 commit comments

Comments
 (0)