記錄

Spring) security + 암호화 세팅 본문

Web/Spring framework

Spring) security + 암호화 세팅

surhommejk 2018. 5. 10. 14:32


pom.xml

    <!-- Spring Security -->

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>4.0.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
      <version>4.0.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-taglibs</artifactId>
      <version>4.0.1.RELEASE</version>
    </dependency>






web.xml

<!-- Root IOC 컨테이너 구성하기 -->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
    /WEB-INF/applicationContext.xml
    /WEB-INF/security-context.xml
   </param-value>
</context-param>

<!-- 인증권한 관련 Filter -->
<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>






security-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           ">

<security:http>
<security:csrf disabled="true"/>
<security:form-login login-page="/joinus/login.htm"
authentication-failure-url="/joinus/login.htm?error" />
<security:logout logout-success-url="/index.htm" />
<!-- Role 검사하고 싶은 요청값 설정과 Role 설정 -->
<security:intercept-url pattern="/customer/*Reg.htm" access="hasRole('ROLE_USER')" />
<security:intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />
</security:http>

<security:authentication-manager>
  <security:authentication-provider>
    <security:jdbc-user-service data-source-ref="driverManagerDataSource"
      users-by-username-query="select userid, pwd as password, 1 enabled from
member where userid=?"
      authorities-by-username-query="select m.userid, r.ROLE_NAME
                      from member m join roll r
                      on m.userid = r.userid
                      where m.userid=?"
    />
    <security:password-encoder ref="bCryptPasswordEncoder"/>
  </security:authentication-provider>
</security:authentication-manager>
</beans>






applicationContext.xml

<bean id="bCryptPasswordEncoder" class="org.springframework.security.crypto.
bcrypt.BCryptPasswordEncoder">
</bean>






Controller 사용 예시

  @Autowired
  private BCryptPasswordEncoder bCryptPasswordEncoder;

  @RequestMapping(value="join.htm",method=RequestMethod.POST)
  public String join(Member member) {
    //회원가입 처리 .... NewMemberDao
    //System.out.println(member.toString());
    member.setPwd(this.bCryptPasswordEncoder.encode(member.getPwd()));
    MemberDao memberdao = sqlsession.getMapper(MemberDao.class);
    try {
        memberdao.insertMember(member);
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    return "redirect:/index.htm"; //수정하면 안되면 페이지 다시 요청
  }





Comments