Skip to content

Instantly share code, notes, and snippets.

@pikanji
pikanji / gist:4562863
Created January 18, 2013 07:01
Spring JDBC dependency for MySQL
<!-- Spring JDBC -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
@pikanji
pikanji / gist:4562891
Created January 18, 2013 07:12
Sample to access MySQL from Spring MVC.
try {
Class.forName("com.mysql.jdbc.Driver"); // Load driver
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/db_name?useUnicode=true&characterEncoding=UTF-8",
"db_user",
"db_password"
);
Statement state = con.createStatement();
String sql = "INSERT INTO `users` (`username`, `password`) VALUES ('hoge', 'fuga')";
@pikanji
pikanji / pom.xml
Created January 19, 2013 09:27
Sample for adding dependency of spring-security to a Spring MVC project.
<!-- Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.1.0.RELEASE</version>
@pikanji
pikanji / web.diff
Last active December 11, 2015 08:09
Registering spring-security to web.xml
- <param-value>/WEB-INF/spring/root-context.xml</param-value>
+ <param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/spring-security.xml</param-value>
@pikanji
pikanji / web.xml
Created January 19, 2013 09:42
Registering spring-security to web.xml (2)
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
@pikanji
pikanji / spring-security.xml
Created January 19, 2013 10:13
Minimum sample of spring-security.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http auto-config="true">
@pikanji
pikanji / home.jsp
Created January 19, 2013 13:46
Sample JSP script to display user name if the variable is not empty.
<c:if test="${not empty userName}">
<p>Logged in as: ${userName}</p>
</c:if>
@pikanji
pikanji / HomeController.java
Last active December 11, 2015 08:19
Sample Spring MVC controller method that passes user name of the user logged in.
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model, Principal principal) {
//...
if (null != principal) {
model.addAttribute("userName", principal.getName());
}
//...
return "home";
}
public static void main(String[] args) {
System.out.println("Enter a string to encode:");
Scanner scan = new Scanner(System.in);
String org = scan.next();
System.out.println("Original String: "+ org);
StandardPasswordEncoder encoder = new StandardPasswordEncoder();
String enc = encoder.encode(org);
CREATE TABLE `users` (
user_id int(10) NOT NULL AUTO_INCREMENT,
user_name varchar(40) NOT NULL,
password text NOT NULL,
enabled tinyint( 1 ) NOT NULL DEFAULT '1',
PRIMARY KEY (user_id)
)