springSecurity初學 安全登陸驗證

一.快速入門:css

    1).導入依賴web

     

<!--版本信息-->
<spring.security.version>5.0.1.RELEASE</spring.security.version>

 <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.security.version}</version>
        </dependency>

   2).web.xml配置spring

      

 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--增長加載spring-security.xml-->
    <param-value>classpath:applicationContext.xml,classpath:spring-security2.xml</param-value>
  </context-param>

 <!--securityFilter-->
  <filter>
    <!--此處name值必須爲springSecurityFilterChain!!!-->
    <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>

  3).spring-security.xml文件配置數據庫

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    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.xsd          
    http://www.springframework.org/schema/security          
    http://www.springframework.org/schema/security/spring-security.xsd">
    
    <!-- 配置不攔截的資源 -->
    <security:http pattern="/login.jsp" security="none"/>
    <security:http pattern="/failer.jsp" security="none"/>
    <security:http pattern="/css/**" security="none"/>
    <security:http pattern="/img/**" security="none"/>
    <security:http pattern="/plugins/**" security="none"/>
    
    <!-- 
        配置具體的規則 
        auto-config="true"    不用本身編寫登陸的頁面,框架提供默認登陸頁面
        use-expressions="false"    是否使用SPEL表達式(沒學習過)
    -->
    <security:http auto-config="true" use-expressions="false">
        <!-- 配置具體的攔截的規則 pattern="請求路徑的規則" access="訪問系統的人,必須有ROLE_USER的角色" -->
        <security:intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN"/>
        
        <!-- 定義跳轉的具體的頁面 -->
        <security:form-login  
            login-page="/login.jsp"
            login-processing-url="/login"  
            default-target-url="/pages/main.jsp"
            authentication-failure-url="/failer.jsp"
        />
        
        <!-- 關閉跨域請求 -->
        <security:csrf disabled="true"/>
        
        <!-- 退出 -->
        <security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp" />
        
    </security:http>
    
    <!-- 切換成數據庫中的用戶名和密碼 -->
    <security:authentication-manager>
        <security:authentication-provider user-service-ref="userService">
            <!-- 配置加密的方式 -->
            <security:password-encoder ref="passwordEncoder"/>
        </security:authentication-provider>
    </security:authentication-manager>
    <!-- 配置加密類 -->
    <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
    
     <!--提供了入門的方式,在內存中存入用戶名和密碼-->
   <!-- <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>-->

 </beans>   
 
 
 

 4)登陸的controller層交給springsecurity框架,須要編寫service層,須要實現UserDetailsService接口express

   service層接口:跨域

  

public interface IUserService extends UserDetailsService {
    
    

 service層實現類:session

@Service("userService")  //名字和spring-security.xml配置對應
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserDao userDao;
    
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder; //加密類

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo = userDao.findByUsername(username);
         //沒有帶狀態的user
//        User user = new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(),getAuthorities());
         //給密碼加密
       /* String encode = bCryptPasswordEncoder.encode(userInfo.getPassword());
        userInfo.setPassword(encode);*/
       
        /*攜帶狀態信息的user*/
        User user = new User(userInfo.getUsername(),userInfo.getPassword(),
                userInfo.getStatus()==0?false:true,true,true,true,
                getAuthorities(userInfo.getRoles()));
        return user;
    }
    //爲了經過驗證,獲取角色信息
    public List<SimpleGrantedAuthority> getAuthorities(List<Role> roleList){
        List<SimpleGrantedAuthority> list =new ArrayList<>();
        for (Role role : roleList) {
            list.add(new SimpleGrantedAuthority(role.getRoleName()));
        }
        return list;
    }
相關文章
相關標籤/搜索