Nginx有關(guān)Session共享,Http安全認(rèn)證,代碼安全鎖等實(shí)戰(zhàn)問題

知識(shí)要點(diǎn):

session共享解決方案實(shí)戰(zhàn)

Http安全認(rèn)證403解決方案

代碼安全鎖解決方案

session解決方案實(shí)戰(zhàn)

  • session粘滯---iphash方式
    在nginx的配置文件nginx.conf
upstream tomcatServer{ 
    #upstream 服務(wù)器組  
    #服務(wù)名稱                權(quán)重    
    server 127.0.0.1:8080   weight=1;
    server 127.0.0.1:8082   weight=1;
    #缺點(diǎn): 同一個(gè)ip訪問同一個(gè)服務(wù)器  會(huì)導(dǎo)致單臺(tái)服務(wù)器壓力過大 
    #ip_hash;
    }
  #放在server塊之前
  • session復(fù)制方案
    在tomcat的conf目錄中server.xml文件下
    添加如下配置
<Engine name="Catalina" defaultHost="localhost">
#在server找到catalina下面添加如下文件:
#解析:該方案以組播的形式配置
 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="8">

          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"/>
          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="auto"
                      port="4000"
                      autoBind="100"
                      selectorTimeout="5000"
                      maxThreads="6"/>

            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/>
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=""/>
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>

還要在web.xml中添加一個(gè)自閉合標(biāo)簽使得我們的配置在web項(xiàng)目中能夠正常使用

<distributable/>
  • redis共享方案
1、pom文件配置中添加
<dependency>
           
<groupId>org.springframework.boot</groupId>
           
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
            
<groupId>org.springframework.session</groupId>
            
<artifactId>spring-session-data-redis</artifactId>
 </dependency>
2、
在application.yml文件中添加
#對(duì)Redis 屬配置 
spring.redis.database=0      
spring.redis.host=127.0.0.1    #設(shè)置ip地址
spring.redis.port=6379        #設(shè)置端口號(hào)
spring.redis.password=        #如果是默認(rèn)的那么密碼為空
#添加注解
#代碼部分修改啟動(dòng)類添加
3、@EnableRedisHttpSession

http安全認(rèn)證后403解決方案

  • 修改訪問權(quán)限
    檢查權(quán)限控制啟動(dòng)用戶和工作用戶是否一致。
    在nginx.conf修改釋放user注釋 修改用戶為root最高權(quán)限。
user  root;            #設(shè)置用戶權(quán)限為root
worker_processes  1;
  • 修改文件權(quán)限
    我們出現(xiàn)403很大一部分原因是因?yàn)槲募?quán)限不足導(dǎo)致的,那么我們就需要修改文件的權(quán)限來(lái)完成。
    方法如下
chmod -R 777 file  # 更改文件的讀寫權(quán)限   -R代表遞歸該目錄下所有文件 file指的是你的文件夾
  • 修改文件所有者
    還有一種可能文件所有者不是nginx nginx無(wú)法讀取到此文件
#是因?yàn)橐L問的文件所屬者不屬于nginx   到文件存儲(chǔ)目錄輸入命令: 
chown nginx:nginx file   # 更改文件的所有者為nginx
  • 修改文件狀態(tài)

代碼安全鎖解決方案

①gitee方案
在gitee中對(duì)代碼進(jìn)行加簽
簽的種類有很多可以在github上進(jìn)行下載 選擇自己適合的(github里面的代碼認(rèn)證部分)

  • 代碼編寫中不寫注釋 不遵守代碼的協(xié)定簡(jiǎn)潔性
  • 多寫點(diǎn)tood注釋 在不同的地方插入不同的數(shù)據(jù)(樹枝填海策略)
  • 更改通用規(guī)定 比如時(shí)間time 我們用user來(lái)定義 修改定義策略
  • 正常重載后的函數(shù),其功能應(yīng)該和被重載的函數(shù)應(yīng)該是接近的,我們要做的就是讓重載后的函數(shù) 和被重載函數(shù)的功能完全沒有關(guān)系。
  • 這個(gè)時(shí)候看代碼的人如果基礎(chǔ)不牢的話,可能需要去溫習(xí)下函數(shù)重載的知識(shí),是不是自己以前理解錯(cuò)了。
  • 混亂#define
  • 樣板示例:
ifndef DONE#ifdef TWICE// put stuff here to declare 3rd time aroundvoidg(char* str);#define DONE#else// TWICE#ifdef ONCE// put stuff here to declare 2nd time around<voidg(void* str);#define TWICE#else// ONCE// put stuff here to declare 1st time aroundvoidg(std::string str);#define ONCE#endif// ONCE#endif// TWICE#endif// DONE

這樣做完之后除了你自己編譯的時(shí)候知道后期無(wú)人知曉
做的時(shí)候記得備份一份清晰的自己看的注釋文檔 ,不然你的代碼后期沒有人會(huì)接。
不過既然采用了單體式服務(wù)那么我相信你的服務(wù)后期必然會(huì)升級(jí) 不建議使用 僅限于部分自用部分!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容