462 solution-code-spring-security-demo-08-jdbc-plaintext
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="[Link]")
@PropertySource("classpath:[Link]")
public class DemoAppConfig {
// set up variable to hold the properties
@Autowired
private Environment env;
// set up a logger for diagnostics
private Logger logger = [Link](getClass().getName());
// define a bean for ViewResolver
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
[Link]("/WEB-INF/view/");
[Link](".jsp");
return viewResolver;
}
1
// define a bean for our security datasource
@Bean
public DataSource securityDataSource() {
// create connection pool
ComboPooledDataSource securityDataSource
= new ComboPooledDataSource();
// set the jdbc driver class
try {
[Link]([Link]("[Link]"));
} catch (PropertyVetoException exc) {
throw new RuntimeException(exc);
}
// log the connection props
// for sanity's sake, log this info
// just to make sure we are REALLY reading data from properties file
[Link](">>> [Link]=" + [Link]("[Link]"));
[Link](">>> [Link]=" + [Link]("[Link]"));
// set database connection props
[Link]([Link]("[Link]"));
[Link]([Link]("[Link]"));
[Link]([Link]("[Link]"));
// set connection pool props
[Link](
getIntProperty("[Link]"));
[Link](
getIntProperty("[Link]"));
[Link](
getIntProperty("[Link]"));
[Link](
getIntProperty("[Link]"));
return securityDataSource;
}
// need a helper method
// read environment property and convert to int
private int getIntProperty(String propName) {
String propVal = [Link](propName);
// now convert to int
int intPropVal = [Link](propVal);
return intPropVal;
}
}
2
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import
[Link]
Builder;
import [Link];
import [Link];
import
[Link]
r;
import [Link];
import [Link];
@Configuration
@EnableWebSecurity
public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {
// add a reference to our security data source
@Autowired
private DataSource securityDataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// use jdbc authentication ... oh yeah!!!
[Link]().dataSource(securityDataSource);
@Override
protected void configure(HttpSecurity http) throws Exception {
[Link]()
.antMatchers("/").hasRole("EMPLOYEE")
.antMatchers("/leaders/**").hasRole("MANAGER")
.antMatchers("/systems/**").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/showMyLoginPage")
.loginProcessingUrl("/authenticateTheUser")
.permitAll()
.and()
.logout().permitAll()
.and()
.exceptionHandling().accessDeniedPage("/access-denied");
3
[Link]
package [Link];
import
[Link]
;
public class MySpringMvcDispatcherServletInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { [Link] };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
[Link]
package [Link];
import [Link];
public class SecurityWebApplicationInitializer
extends AbstractSecurityWebApplicationInitializer {
4
[Link]
package [Link];
import [Link];
import [Link];
@Controller
public class DemoController {
@GetMapping("/")
public String showHome() {
return "home";
}
// add request mapping for /leaders
@GetMapping("/leaders")
public String showLeaders() {
return "leaders";
}
// add request mapping for /systems
@GetMapping("/systems")
public String showSystems() {
return "systems";
}
[Link]
package [Link];
import [Link];
import [Link];
@Controller
public class LoginController {
@GetMapping("/showMyLoginPage")
public String showMyLoginPage() {
// return "plain-login";
return "fancy-login";
// add request mapping for /access-denied
@GetMapping("/access-denied")
public String showAccessDenied() {
return "access-denied";
}
5
[Link]
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>com.luv2code</groupId>
<artifactId>spring-security-demo</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>spring-security-demo</name>
<properties>
<[Link]>[Link]</[Link]>
<[Link]>[Link]</[Link]>
<[Link]>1.8</[Link]>
<[Link]>1.8</[Link]>
</properties>
<dependencies>
<!-- Spring MVC support -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${[Link]}</version>
</dependency>
<!-- Spring Security -->
<!-- spring-security-web and spring-security-config -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-security-web</artifactId>
<version>${[Link]}</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-security-config</artifactId>
<version>${[Link]}</version>
</dependency>
<!-- Add Spring Security Taglibs support -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${[Link]}</version>
</dependency>
<!-- Add MySQL and C3P0 support -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
6
<artifactId>c3p0</artifactId>
<version>[Link]</version>
</dependency>
<!-- Servlet, JSP and JSTL support -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>[Link]-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>[Link]-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- TO DO: Add support for Maven WAR Plugin -->
<build>
<finalName>spring-security-demo</finalName>
<pluginManagement>
<plugins>
<plugin>
<!-- Add Maven coordinates (GAV) for: maven-war-plugin -->
<groupId>[Link]</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
7
[Link]
<%@ taglib prefix="form" uri="[Link] %>
<%@ taglib prefix="security" uri="[Link] %>
<html>
<head> <title>luv2code Company Home Page</title> </head>
<body>
<h2>luv2code Company Home Page</h2>
<hr>
<p>
Welcome to the luv2code company home page!
</p>
<hr>
<!-- display user name and role -->
<p>
User: <security:authentication property="[Link]" />
<br><br>
Role(s): <security:authentication property="[Link]" />
</p>
<security:authorize access="hasRole('MANAGER')">
<!-- Add a link to point to /leaders ... this is for the managers -->
<p>
<a href="${[Link]}/leaders">Leadership
Meeting</a>
(Only for Manager peeps)
</p>
</security:authorize>
<security:authorize access="hasRole('ADMIN')">
<!-- Add a link to point to /systems ... this is for the admins -->
<p>
<a href="${[Link]}/systems">IT Systems
Meeting</a>
(Only for Admin peeps)
</p>
</security:authorize>
<hr>
<!-- Add a logout button -->
<form:form action="${[Link]}/logout"
method="POST">
<input type="submit" value="Logout" />
</form:form>
</body>
</html>
8
[Link]
<%@ taglib prefix="form" uri="[Link] %>
<%@ taglib prefix="c" uri="[Link] %>
<html>
<head>
<title>Custom Login Page</title>
<style>
.failed {
color: red;
}
</style>
</head>
<body>
<h3>My Custom Login Page</h3>
<form:form action="${[Link]}/authenticateTheUser"
method="POST">
<!-- Check for login error -->
<c:if test="${[Link] != null}">
<i class="failed">Sorry! You entered invalid username/password.</i>
</c:if>
<p>
User name: <input type="text" name="username" />
</p>
<p>
Password: <input type="password" name="password" />
</p>
<input type="submit" value="Login" />
</form:form>
</body>
</html>
9
[Link]
<html>
<head>
<title>luv2code LEADERS Home Page</title>
</head>
<body>
<h2>luv2code LEADERS Home Page</h2>
<hr>
<p>
See you in Brazil ... for our annual Leadership retreat!
<br>
Keep this trip a secret, don't tell the regular employees LOL :-)
</p>
<hr>
<a href="${[Link]}/">Back to Home Page</a>
</body>
</html>
[Link]
<html>
<head>
<title>luv2code SYSTEMS Home Page</title>
</head>
<body>
<h2>luv2code SYSTEMS Home Page</h2>
<hr>
<p>
We have our annual holiday Caribbean cruise coming up. Register now!
<br>
Keep this trip a secret, don't tell the regular employees LOL :-)
</p>
<hr>
<a href="${[Link]}/">Back to Home Page</a>
</body>
</html>
10
[Link]
<html>
<head>
<title>luv2code - Access Denied</title>
</head>
<body>
<h2>Access Denied - You are not authorized to access this resource.</h2>
<hr>
<a href="${[Link]}/">Back to Home Page</a>
</body>
</html>
11
[Link]
<%@ taglib prefix="form" uri="[Link] %>
<%@ taglib prefix="c" uri="[Link] %>
<!doctype html>
<html lang="en">
<head>
<title>Login Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Reference Bootstrap files -->
<link rel="stylesheet" href="[Link]
<script src="[Link]
<script src="[Link]
</head>
<body>
<div>
<div id="loginbox" style="margin-top: 50px;"
class="mainbox col-md-3 col-md-offset-2 col-sm-6 col-sm-offset-2">
<div class="panel panel-info">
<div class="panel-heading">
<div class="panel-title">Sign In</div>
</div>
<div style="padding-top: 30px" class="panel-body">
12
<!-- Login Form -->
<form action="${[Link]}/authenticateTheUser"
method="POST" class="form-horizontal">
<!-- Place for messages: error, alert etc ... -->
<div class="form-group">
<div class="col-xs-15">
<div>
<!-- Check for login error -->
<c:if test="${[Link] != null}">
<div class="alert alert-danger col-xs-offset-1 col-xs-10">
Invalid username and password.
</div>
</c:if>
<!-- Check for logout -->
<c:if test="${[Link] != null}">
<div class="alert alert-success col-xs-offset-1 col-xs-10">
You have been logged out.
</div>
</c:if>
</div>
</div>
</div>
13
<!-- User name -->
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" name="username" placeholder="username" class="form-control">
</div>
<!-- Password -->
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input type="password" name="password" placeholder="password" class="form-control" >
</div>
<!-- Login/Submit Button -->
<div style="margin-top: 10px" class="form-group">
<div class="col-sm-6 controls">
<button type="submit" class="btn btn-success">Login</button>
</div>
</div>
<!-- I'm manually adding tokens ... Bro! -->
<input type="hidden"
name="${_csrf.parameterName}"
value="${_csrf.token}" />
</form>
</div>
</div>
</div>
</div>
</body>
</html>
14