SpingSecurity 3 Formログイン
初めてSpringSecurityに触れて、本当に牛Bのものだと感嘆するしかありません.マークちょっと
SpringSecurityを使用するには、まずJarパッケージを追加し、Mavenに次の依存を追加します.
それからweb.xmlにSpringSecurityのフィルタを追加します
SpringSecurityのプロファイルを追加
classpathの下にspring-security.xmlファイルを新規作成します.セキュリティ設定はspring-securityに配置されます.
カスタム・データベース・テーブルを使用するには、UserDetailServiceをカスタマイズする必要があります.現在は比較的小さいため、データベースにはユーザー名、パスワード、ユーザーに対応するロール名しか指定されていません.
1つのフォームにログインすると、ユーザー名に基づいてユーザー認証情報を取得するカスタムUserDetailのメソッドが呼び出されます.
フロントログインボックス、対応formは以下の通りです
SpringSecurityを使用するには、まずJarパッケージを追加し、Mavenに次の依存を追加します.
- <!-- spring3 Security -->
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-core</artifactId>
- <version>3.1.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-web</artifactId>
- <version>3.1.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-config</artifactId>
- <version>3.1.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.security</groupId>
- <artifactId>spring-security-taglibs</artifactId>
- <version>3.1.2.RELEASE</version>
- </dependency>
それからweb.xmlにSpringSecurityのフィルタを追加します
- <!-- Spring Security -->
- <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>
SpringSecurityのプロファイルを追加
- <!-- spring -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring.xml,classpath:spring-hibernate.xml,classpath:spring-security.xml</param-value>
- </context-param>
classpathの下にspring-security.xmlファイルを新規作成します.セキュリティ設定はspring-securityに配置されます.
- <?xml version="1.0" encoding="UTF-8"?>
- <beans:beans xmlns="http://www.springframework.org/schema/security"
- xmlns:beans="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
- <debug/>
- <!-- css js -->
- <http pattern="*.css" security="none"></http>
- <http pattern="*.js" security="none"></http>
-
-
- <http use-expressions="true" auto-config="true">
- <!-- patten Url , ant , use-expressions access -->
- <intercept-url pattern="/index.jsp" access="hasRole('ROLE_ADMIN')"/>
- <intercept-url pattern="/**/*.action" access="hasRole('ROLE_ADMIN')"/>
- <!-- , Cookie -->
- <logout delete-cookies="JSESSIONID"/>
- <!-- , /login.jsp -->
- <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true"/>
- <!-- -->
- <session-management>
- <!-- Session -->
- <concurrency-control max-sessions="10" expired-url="/CommonAction!login.action"/>
- </session-management>
- <!-- , 1 -->
- <remember-me token-validity-seconds="84000"/>
- </http>
-
- <!-- -->
- <beans:bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"/>
-
- <beans:bean name="myUserDetilService" class="com.graduate.security.UserFoundService">
- <beans:property name="teacherDao" ref="TeacherDao"></beans:property>
- </beans:bean>
-
-
- <authentication-manager>
- <authentication-provider user-service-ref="myUserDetilService"/>
- </authentication-manager>
-
- </beans:beans>
カスタム・データベース・テーブルを使用するには、UserDetailServiceをカスタマイズする必要があります.現在は比較的小さいため、データベースにはユーザー名、パスワード、ユーザーに対応するロール名しか指定されていません.
- package com.graduate.security;
-
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Set;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.GrantedAuthorityImpl;
- import org.springframework.security.core.userdetails.User;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.core.userdetails.UserDetailsService;
- import org.springframework.security.core.userdetails.UsernameNotFoundException;
-
- import com.graduate.dao.TeacherDao;
- import com.graduate.models.Teacher;
- import com.graduate.models.Usertype;
-
- public class UserFoundService implements UserDetailsService{
-
- private TeacherDao teacherDao;
-
- private Logger logger=LoggerFactory.getLogger(UserFoundService.class);
-
- @Override
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
- Teacher teacher = teacherDao.getTeacherByName(username);
- UserDetails user=null;
- if(teacher!=null){
- String password=teacher.getPassword();
- String name=teacher.getName();
- List<GrantedAuthority> authority=getTeacherAuthority(teacher);
- user=new User(name,password,authority);
- logger.info(" , "+name);
- return user;
- }else{
- logger.info(" ");
- throw new UsernameNotFoundException(" ");
- }
- }
-
-
-
-
- private List<GrantedAuthority> getTeacherAuthority(Teacher teacher) {
- List<GrantedAuthority> grantedAuthority=new ArrayList<GrantedAuthority>();
-
- Set<Usertype> roleType = teacher.getRoleType();
-
- for(Usertype type:teacher.getRoleType()){
- System.out.println(type.getName());
- GrantedAuthority auth=new GrantedAuthorityImpl(type.getName());
- grantedAuthority.add(auth);
- }
- return grantedAuthority;
- }
-
-
- public TeacherDao getTeacherDao() {
- return teacherDao;
- }
-
- public void setTeacherDao(TeacherDao teacherDao) {
- this.teacherDao = teacherDao;
- }
-
-
- }
1つのフォームにログインすると、ユーザー名に基づいてユーザー認証情報を取得するカスタムUserDetailのメソッドが呼び出されます.
フロントログインボックス、対応formは以下の通りです
- <form id="loginForm" method="post" action="j_spring_security_check">
-
-
- <c:if test="${param.error eq 'true'}"><p><label style="color: red; margin-bottom: 10px;"> , </label></p></c:if>
- <p>
- <label for="user_name"> <br>
- </label>
- <input type="text" size="20" value="${sessionScope['SPRING_SECURITY_LAST_USERNAME']}" class="validate[required,minSize[2],maxSize[10] ] inpu" id="user_name" name="j_username" />
- </p>
- <p>
- <label for="user_pass"> <br>
- <input type="password" size="20" value="" class="validate[required,minSize[5],maxSize[12]] inpu" id="user_pass" name="j_password" /></label>
- </p>
- <div class="clearfix">
- <p class="forgetmenot">
- <label for="rememberme"><input type="checkbox" tabindex="90" id="_spring_security_remember_me" name="_spring_security_remember_me"> </label>
- </p>
- <p class="nore">
- <a href="#"> ?</a>
- </p>
- </div>
- <p>
- <a class="login_btn" href="javascript:;" onclick='$("#loginForm").submit()'> </a>
- </p>
- </form>