Skip to content

Commit

Permalink
Add security and connection pooling
Browse files Browse the repository at this point in the history
  • Loading branch information
mzkhan committed Apr 19, 2015
1 parent eb45205 commit e688f2a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,15 @@
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
</dependencies>
</project>
9 changes: 4 additions & 5 deletions src/com/markz/testApp/config/DataConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.sql.DataSource;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
Expand All @@ -16,14 +17,12 @@ public class DataConfig {

@Bean
public DataSource dataSource() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass(com.mysql.jdbc.Driver.class);
String driverClass = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://enliteinstance.c9a1yjg7i5cn.us-east-1.rds.amazonaws.com:3306/test";
String username = "master";
String password = "enliteUs";
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
PooledDataSource dataSource = new PooledDataSource(driverClass, url, username, password);
// dataSource.setPoolMaximumActiveConnections(20);
return dataSource;
}

Expand Down
30 changes: 30 additions & 0 deletions src/com/markz/testApp/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.markz.testApp.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;


@Configuration
@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("zuber").password("test").roles("USER");

}

// @Autowired
// public void configure(HttpSecurity http) throws Exception {
// http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
// .and()
// .authorizeRequests()
// .regexMatchers("/users").access("read");
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.markz.testApp.config;

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

public class SecurityWebApplicationInitializer extends
AbstractSecurityWebApplicationInitializer {

public SecurityWebApplicationInitializer() {
super(SecurityConfig.class);
}

}
4 changes: 3 additions & 1 deletion src/com/markz/testApp/controller/MyTestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -54,7 +55,8 @@ public void createUser(@RequestBody UserDetail userDetail,

@RequestMapping(value = "/user/update/{id}", method = RequestMethod.PUT)
public UserDetail updateUser(@RequestBody UserDetail userDetail,
@PathVariable int id, HttpServletResponse httpServletResponse) {
@PathVariable int id, HttpServletResponse httpServletResponse,
AuthenticationException authException) {
LOGGER.info("Updating the user with id: " + id);

try {
Expand Down
2 changes: 1 addition & 1 deletion src/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Root logger option
log4j.rootLogger=INFO, stdout
log4j.rootLogger=DEBUG, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
Expand Down

0 comments on commit e688f2a

Please sign in to comment.