아무거나

[spring] 보안 Security - 1 본문

Java/Spring Security

[spring] 보안 Security - 1

전봉근 2019. 12. 26. 01:07
반응형

[spring] 보안 Security - 1

 

1. 보안 관련 라이브러리 추가

   방법 1. https://docs.spring.io/spring-security/site/docs/5.0.6.RELEASE/reference/htmlsingle/ 에서 다운받거나 dependency 추가

   방법 2. 이클립스 기준 pom.xml -> 하단 Dependencies탭 클릭 -> Add.. 클릭 -> Enter groupId, artifactId or sha1 prefix ... 에 security 검색

           -> org.springframework.security(spring-security-acl) 선택

 

   (1) security-context.xml 생성 ( src/main/webapp/WEB-INF/spring/appServlet/security-context.xml )

       [내용]

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
</beans>

 

* 해당경로에 오른쪽 클릭해서 xml생성을해서 next를 누르면 사용할 bean을 선택한다

  security를 체크하고 맨위의 최신버전을 체크해서 사용하자.

 

   (2) web.xml에 security-context.xml를 추가하자. ( src/main/webapp/WEB-INF/web.xml )

       [web.xml]

....

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml
        /WEB-INF/spring/appServlet/security-context.xml
    </param-value>
</context-param>

....

 

2. In-Memory 인증

   (1) security-context.xml 생성 후 내용추가 ( src/main/webapp/WEB-INF/spring/appServlet/security-context.xml ) 

       [내용]

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<security:http auto-config="true">
    <!-- 페이지 별로 접근제어 ex: ROLE_USER -->
    <security:intercept-url pattern="/login.html*" access="ROLE_USER"/>
    <security:intercept-url pattern="/welcome.html*" access="ROLE_ADMIN"/>
</security:http>
<security:authentication-manager>
    <security:authentication-provider>
        <security:user-service>
            <!-- login.html에 접근시 아래 name, password대로 입력해야 통과된다. -->
            <security:user name="user" password="123" authorities="ROLE_USER"/>
            <!-- 아래 룰은 admin유저가 ROLE_ADMIN,ROLE_USER 둘다 접근이 가능하다 -->
            <security:user name="admin" password="123" authorities="ROLE_ADMIN,ROLE_USER"/>
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>
</beans>

 

   (2) 필터추가 ( src/main/webapp/WEB-INF/web.xml )

       [web.xml]

....

<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>

.... 

 

 

참고: https://www.inflearn.com/course/%EC%9E%90%EB%B0%94-%EC%8A%A4%ED%94%84%EB%A7%81-%EA%B0%95%EC%A2%8C/dashboard

반응형
Comments