기존 에러 페이지
protected void configure(HttpSecurity http) throws Exception {
//커트에러 페이지 생성하기
http.exceptionHandling().accessDeniedPage("/access-denied");
컨트롤러 연결하기
access-denied.jsp 만들기
접근성규제 access 완료
<security:authorize access = "hasRole('MANAGER')">
<a href="${pageContext.request.contextPath}/leaders" style = "text-decoration: none;">Leadership Meeting</a>(Only for Managers)<br>
<input type = "button" value = "Leadership Meeting" class ="add-button"
onclick="window.location.href='leaders'; return false;"/>
</security:authorize>
inMemoryAuthentication아닌 DB에서
DROP DATABASE IF EXISTS `spring_security_demo_plaintext`;
CREATE DATABASE IF NOT exists `spring_security_demo_plaintext`;
USE `spring_security_demo_plaintext`;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users`(
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`enabled` tinyint(1) NOT NULL,
PRIMARY KEY (`username`) )
ENGINE = InnoDB DEFAULT CHARSET = latin1;
INSERT INTO `users`
VALUES
('john','{noop}test123',1),
('mary','{noop}test123',1),
('susan','{noop}test123',1);
DROP TABLE IF EXISTS `authorities`;
CREATE TABLE `authorities`(
`username` varchar(50) NOT NULL,
`authority` varchar(50) NOT NULL,
UNIQUE KEY `authorities_idx_1`(`username`,`authority`),
CONSTRAINT`authorities_ibfk_1`FOREIGN KEY(`username`)
REFERENCES `users`(`username`))
ENGINE = InnoDB DEFAULT CHARSET = latin1;
INSERT INTO `authorities`
VALUES
('john','ROLE_EMPLOYEE'),
('mary','ROLE_EMPLOYEE'),
('mary','ROLE_MANAGER'),
('susan','ROLE_EMPLOYEE'),
('susan','ROLE_ADMIN');
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
persistence-mysql.properties 추가하기
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_security_demo_plaintext?useSSL=false&serverTimezone=UTC
jdbc.user=springstudent
jdbc.password=springstudent
#
#Connection pool properties
#
connection.pool.initialPoolSize=5
connection.pool.minPoolSize=5
connection.pool.maxPoolSize=20
connection.pool.maxIdleTime=3000
package com.code.springsecurity.demo.config;
import java.beans.PropertyVetoException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration //설정파일을 만들기 위한 애노테이션 or Bean을 등록하기 위한 애노테이션이다.
@EnableWebMvc //이부분 MVC
@ComponentScan(basePackages="com.code.springsecurity.demo") //구성요소를 스캔하겠다
@PropertySource("classpath:persistence-mysql.properties") //디비설정과 연결하곘다
public class DemoAppConfig implements WebMvcConfigurer{
@Autowired
private Environment env;
@Bean //@Configuration과 함께 사용하며, 자바클래스 내에 그 종속성을 매스드화(인스턴스화)하는데 사용한다
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public DataSource securityDataSource() {
ComboPooledDataSource securityDataSource = new ComboPooledDataSource();
//jdbc 커넥션 풀설정
try {
securityDataSource.setDriverClass(env.getProperty("jdbc.driver"));
} catch (PropertyVetoException e) {
e.printStackTrace();
}
securityDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
securityDataSource.setUser(env.getProperty("jdbc.user"));
securityDataSource.setPassword(env.getProperty("jdbc.password"));
securityDataSource.setInitialPoolSize(getInt("connection.pool.initialPoolSize"));
securityDataSource.setMinPoolSize(getInt("connection.pool.minPoolSize"));
securityDataSource.setMaxPoolSize(getInt("connection.pool.maxPoolSize"));
securityDataSource.setMaxIdleTime(getInt("connection.pool.maxIdleTime"));
return securityDataSource;
}
private int getInt(String st) {
int in = Integer.parseInt(env.getProperty(st));
return in;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
}
}
DemoAppConfig.java
import java.util.logging.Logger;
private Logger logger = Logger.getLogger(getClass().getName());
logger.info(">>>>>jdbc.url===" + env.getProperty("jdbc.driver"));
logger.info(">>>>>jdbc.url===" + env.getProperty("jdbc.url"));