[JAVA] Operation Spring Awakening Spring MVC Part 2

Handle Spring Security

I added spring-security to that one.

Don't forget _φ (・ _ ・ ・

Somehow it is in a state, but I cut and pasted a sample of the world and managed to make it work. This is like a liver.

    <http auto-config="true" >
        <!--Authorization settings-->
        <intercept-url pattern="/top*" access="hasAnyRole('ROLE_ADMIN', 'ROLE_USER')" />
        <intercept-url pattern="/admin*" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/list*" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/user*" access="hasRole('ROLE_USER')" />

        <!--Transition destination when there is no authority-->
        <access-denied-handler error-page="/403" />

        <!--Authentication login process-->
        <form-login
            login-page="/"
            default-target-url="/top"
            authentication-failure-url="/error"
            login-processing-url="/j_spring_security_check"/>

        <!--Authentication logout process-->
        <logout
            logout-url="/logout"
            logout-success-url="/"
            invalidate-session="true"/>
        <!--ROLE for anonymous users-->
        <anonymous granted-authority="ROLE_ANONYMOUS" />
    </http>

In the sample that I used as a reference, ID / password authentication is performed with "security-context.xml", but at that time, the web application and DB are connected. Well, this is natural, but when I described the setting process to connect the Web application and DB again after logging in in "servlet-context.xml", an error occurred. The content seemed to mean "I made a connection between the web application and the DB earlier, so I can't make the second one ~ (# ゚ Д ゚) ノ". I had no choice but to create a DB setting that is commonly used in "applicationContext.xml" and it worked smoothly.

For reference, I made the DB like this.

CREATE TABLE `todo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` varchar(50) DEFAULT NULL,
  `done` tinyint(1) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8

CREATE TABLE `users` (
  `name` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  `authority` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

The source is here. https://github.com/pugachev/TodoApp.git

This is a site for checking the operation. http://ikefukurou.com/TodoApp/

Recommended Posts

Operation Spring Awakening Spring MVC Part 1
Operation Spring Awakening Spring MVC Part 4
Operation Spring Awakening Spring MVC Part 3
Operation Spring Awakening Spring MVC Part 2
Java Config with Spring MVC
Implement file download with Spring MVC
Spring Boot starting from zero Part 2
Spring Boot starting from zero Part 1