일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- JPQL
- DBMS
- pat발급
- 르세라핌
- ANTIFRAGILE
- gtihub
- java
- 오블완
- spring
- docker
- Python
- 애널리틱스4
- 11월순위
- IntelliJ
- docker 명령어
- db종류
- 도커이미지
- bigquery
- codeium
- JPA
- git pat
- ci/cd
- 데이터내보내기
- git branch 삭제
- 티스토리챌린지
- datagrip
- 명령어
- analytics4
- 컬렉션프레임워크
- Today
- Total
hanker
Spring Security (3) - JDBC 설정 (xml) 본문
1. security-context.xml 수정
<!-- <security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="user" password="password" authorities="ROLE_USER" />
<security:user name="admin" password="password" authorities="ROLE_ADMIN" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager> -->
<security:authentication-manager>
<security:authentication-provider>
<security:jdbc-user-service data-source-ref="dataSource"
role-prefix=""
users-by-username-query="SELECT username, password, enabled FROM users WHERE username = ?"
authorities-by-username-query="SELECT username, authority FROM authorities WHERE username = ?"
/>
</security:authentication-provider>
</security:authentication-manager>
- 기존에 임시로 지정해줬던 user, admin 계정을 주석처리 후 jdbc 연결설정 코드 추가
- (dataSource 추가)
- users 테이블 추가 Column = username, password, enabled
- 테이블 값 insert
- username : admin00 , password : pw00, enabled : 1
- ex) insert into users(username, password, enabled) values('admin00', 'pw00', '1');
insert into users(username, password, enabled) values('user00', 'pw00', '1');
- authorities 테이블 추가 Column = username, authority
- username : admin00, authority
- ex) insert into authorities(username, authority) values('admin00', 'ROLE_ADMIN');
insert into authorities(username, authority) values('user00', 'ROLE_USER');
2. pom.xml 추가
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
3. root-context.xml 추가
<!-- mysql 설정 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/'디비명'?useSSL=false&serverTimezone=UTC"></property>
<property name="username" value="MYSQL Id"></property>
<property name="password" value="MYSQL Pw"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
- dataSource 설정 후 테스트 실행
4. src/text/java MySqlConnectionTest.class 생성
public class MySqlConnectionText {
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String URL = "jdbc:mysql://127.0.0.1:3306/디비명?useSSL=false&serverTimezone=UTC";
private static final String USER = "id";
private static final String PASSWORD = "password";
@Test
public void testConnection() throws Exception {
Class.forName(DRIVER);
try(Connection con = DriverManager.getConnection(URL, USER, PASSWORD)){
System.out.println(con);
} catch(Exception e) {
e.printStackTrace();
}
}
}
- 접속 성공 시 콘솔창
- 테스트까지 성공했으면 접속 테스트
- MySQL 테이블에 있는 접속 계정정보로 로그인 성공
'SPRING' 카테고리의 다른 글
SPRING - mybatis에서 resultType=boolean 사용하기 (0) | 2020.11.02 |
---|---|
SPRING - openlayers 지도보기, mouseOver 좌표확인 (0) | 2020.08.19 |
SPRING - VO클래스 alias 설정 (0) | 2020.08.18 |
Spring Security (2) - 로그인, 로그아웃 설정 (xml) (0) | 2020.08.13 |
Spring Security (1) - 기본 설정 (xml) (0) | 2020.08.13 |