JavaEE學習日誌(一百一十六): 品優購項目筆記(一)

springSecurity的注意事項

在分佈式項目中,使用springSecurity的注意事項

  1. springSecurity如果需要去數據庫中找對應的用戶對象進行驗證, 就需要自定義UserDetailService實現類
  2. 因爲在自定義的service中需要使用其他的service到數據庫中查詢, 所以需要注入, 而這裏sellerService是在service-sellerGoods項目中, 跨項目調用需要使用dubbo注入.
  3. 在springMvc.xml中雖然配置了包掃描但是掃描的是controller包, 只有在這個包下面寫@Refrence註解纔會生效
  4. 在我們UserDetailService實現類中無法使用dubbo註解進行注入(無法進行多次掃描,這是dubbo的bug), 所以我們這裏利用xml配置文件的標籤<dubbo:refrence>進行注入sellerService
  5. 在userDetailService中如果使用@Service註解無法指定注入的屬性或者是對象, 所以這裏我們使用最原始的set方法注入, 可以在配置文件中配置bean, 在bean標籤了配置<property>標籤注入SellerService屬性
package cn.itcast.core.service;

import cn.itcast.core.pojo.seller.Seller;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
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 java.util.ArrayList;
import java.util.List;

/**
 * 自定義驗證類,實現springSecurity框架的userDetailService接口
 */
public class UserDetailServiceImpl implements UserDetailsService {

    private SellerService sellerService;

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<GrantedAuthority> authList = new ArrayList<>();
        authList.add(new SimpleGrantedAuthority("ROLE_SELLER"));

        //1.判斷用戶名是否爲空
        if (username==null){
            return null;
        }
        //2.根據用戶名到數據庫查詢對應的用戶對象
        Seller seller = sellerService.findOne(username);
        //3.如果查不到
        if(seller != null){
            if("1".equals(seller.getStatus())){


                return new User(username,seller.getPassword(),authList);
            }
        }


        return null;
    }
}

<?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"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
	
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
						http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
	
	<http pattern="/*.html" security="none"/>
	<http pattern="/css/**" security="none"/>
	<http pattern="/img/**" security="none"/>
	<http pattern="/js/**" security="none"/>
	<http pattern="/plugins/**" security="none"/>
	<http pattern="/seller/add.do" security="none"/>
	
	<!-- use-expressions:設置是否啓動SpEL表達式,默認值是true。 -->
	<http use-expressions="false">
		<!-- 
			配置SpringSecurity的攔截路徑(攔截規則) 
			* pattern:配置攔截規則。   /* 代表的是根路徑下的所有資源(不包含子路徑) /**代表的是根路徑下所有的資源(包含子路徑)
			* access:設置角色  角色命名 ROLE_角色名稱  如:  ROLE_USER  
		-->
		<intercept-url pattern="/**" access="ROLE_SELLER"/>
		
		<!-- 
		開啓表單驗證 
			username-parameter="username" 
			password-parameter="password" 
			login-page			:登錄頁面名稱  以  / 開始
			default-target-url	:登錄成功後跳轉的頁面
			login-processing-url:提交的路徑的設置 默認值"/login" 可以修改
		-->
		<form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" always-use-default-target="true" authentication-failure-url="/shoplogin.html"/>
		
		<!-- 不使用csrf的校驗 -->
		<csrf disabled="true"/>
		
		<!-- 配置框架頁面不攔截 -->
		<headers>
			<frame-options policy="SAMEORIGIN"/>
		</headers>
		
		<!-- 註銷的配置 -->
		<logout logout-url="/logout" logout-success-url="/shoplogin.html" />
	</http>
	
	<!-- 配置認證管理器 -->
	<authentication-manager>
		<!-- 認證的提供者 -->
		<authentication-provider user-service-ref="userDetailService">
<!--			<password-encoder ref="passwordEncoder"></password-encoder>	-->
		</authentication-provider>
	</authentication-manager>
		

	<!-- 引用dubbo 服務 -->	
	<dubbo:application name="pinyougou-shop-web" />
	<dubbo:registry address="zookeeper://192.168.200.128:2181"/>
	<dubbo:reference id="sellerService"  interface="cn.itcast.core.service.SellerService" >
	</dubbo:reference>
	
	<!-- 配置自定義的認證類 -->
	<beans:bean id="userDetailService" class="cn.itcast.core.service.UserDetailServiceImpl">
		<beans:property name="sellerService" ref="sellerService"></beans:property>
	</beans:bean>


	<beans:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
</beans:beans>

springSecurity的登陸流程

在這裏插入圖片描述

自關聯表

自關聯表, 自己關聯自己, 這張表有主鍵id唯一, 有parentid字段指定父級的主鍵id
自關聯表

  • 優點: 一張表可以通過這種關係做成一個樹狀關係
  • 缺點: 在這張表數據量大的時候, 如果想展示整棵樹所有節點數據加載會非常慢.

表的縱切

使用場景: 一張表字段太多, 幾十個甚至上百個字段, 沒查詢一條或者多條的時候, 返回數據太多,頁面展示不下, 所以沒有必要一次性返回過都數據, 這個時候可以根據頁面需要展示的數據, 將一張表切分成多張表, 多張表的關係是一對一.

  • 優點: 每次查詢的時候返回數據量會變小, 提高網絡io和磁盤io效率. 查詢速度變快
  • 缺點: 如果確實需要一次返回所有數據, 返回的數據量依然大, 增加表關係的複雜度. 不利於sql語句的編寫.

spu和sku

  • SPU標準產品單位: 就是一個產品的統稱, 是一個商品的單位
  • SKU庫存量單位: 一個產品的所有型號, 所有價錢, 所有規格都列出來, 具體的詳細的每一個單位, 也就是庫存單位

SPU: iphone8
SKU: iphone8 64G 8000元 白色 聯通
iphone8 64G 8000元 黑色 聯通
iphone8 128G 9000元 黑色 聯通
iphone8 128G 9000元 白色 聯通
iphone8 64G 8000元 白色 移動
iphone8 64G 8000元 黑色 移動
iphone8 128G 9000元 黑色 移動
iphone8 128G 9000元 白色 移動
一個SPU單位對應多個SKU單位

在這裏插入圖片描述

商品添加五級聯動

angular的下拉框
在這裏插入圖片描述

<select class="form-control" ng-model="entity.goods.category1Id" 
	ng-options="xxx.id as xxx.name for xxx in itemCat1List">	
  • ng-model: 將下拉框選中的數據, 封裝到$scope域中變量叫做entity.goods.category1Id
  • ng-option:指定下來選項的各項內容, 它的語法是for循環itemCat1List這個集合變量, 每次循環賦值給xxx這個變量xxx.id作爲key xxx.name作爲value, 選中一個後, 提交的數據是key, 這裏as , for, in 都是語法關鍵字

angular的內置對象$watch

// 查詢二級分類列表:
	$scope.$watch("entity.goods.category1Id",function(newValue,oldValue){
		itemCatService.findByParentId(newValue).success(function(response){
			$scope.itemCat2List = response;
		});
	});

$watch是angularjs中的內置對象, 作用是監聽$scope域中的某個變量發生了值的改變.

富文本編輯器

我們這裏使用的是kindEditor這個. 其實就是一款網頁版的word, 可以對文字圖片等進行排版功能.都是使用js+css實現的, 如果我們保存富文本編輯器裏面的數據, 保存到數據庫中是 文字+html+css

  • KindEditor: http://kindeditor.net/
  • UEditor: http://ueditor.baidu.com/website/
  • CKEditor: http://ckeditor.com/

引入js和css

<!-- 富文本編輯器 -->
	<link rel="stylesheet" href="../plugins/kindeditor/themes/default/default.css" />
	<script charset="utf-8" src="../plugins/kindeditor/kindeditor-min.js"></script>
	<script charset="utf-8" src="../plugins/kindeditor/lang/zh_CN.js"></script>

添加textarea,name設置爲content

 <textarea name="content" style="width:800px;height:400px;visibility:hidden;" ></textarea>

添加js

            <!-- 正文區域 /-->
<script type="text/javascript">

	var editor;
	KindEditor.ready(function(K) {
		editor = K.create('textarea[name="content"]', {
			allowFileManager : true
		});
	});

</script>

在這裏插入圖片描述

JVM中堆的內部結構

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章