分类 后端 下的文章

go module 的目的是依赖管理,所以使用 go module 时你可以舍弃 go get 命令(但是不是禁止使用, 如果要指定包的版本或更新包可使用go get,平时没有必要使用)

因go的网络问题, 所以推荐使用 goproxy.cn设置

// 阿里云镜像
GOPROXY=https://mirrors.aliyun.com/goproxy/
// 中国golang镜像
GOPROXY=https://goproxy.io
// 七牛云为中国的gopher提供了一个免费合法的代理goproxy.cn,其已经开源。只需一条简单命令就可以使用该代理:

go env -w GOPROXY=https://goproxy.cn,direct

设置GO111MODULE环境变量

要使用go module 首先要设置GO111MODULE=on,GO111MODULE 有三个值,off、on、auto,off 和 on 即关闭和开启,auto 则会根据当前目录下是否有 go.mod 文件来判断是否使用 modules 功能。无论使用哪种模式,module 功能默认不在 GOPATH 目录下查找依赖文件,所以使用 modules 功能时请设置好代理。
在使用 go module 时,将 GO111MODULE 全局环境变量设置为 off,在需要使用的时候再开启,避免在已有项目中意外引入 go module。

windows:

   set GO111MODULE=on

mac:

export GO111MODULE=on

然后输入

go env

查看 GO111MODULE 选项为 on 代表修改成功

package cn.rivamed.config.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
   
   @Override
   protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable()
         .authorizeRequests()
         .anyRequest().permitAll()
         .and().logout().permitAll();
   }
   
}

package com.httpServer;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static void main(String[] args) throws Exception {
        //开启一个服务端Socket,监听12345端口
        ServerSocket server = new ServerSocket(12345);

        //有客户端连进来
        Socket client = server.accept();

        //获取到客户端输入流
        InputStream in = client.getInputStream();

        //准备一个缓冲数组
        byte data[]=new byte[4096];

        //这里有一个read(byte[] b)方法,将数据读取到字节数组中,同返回读取长度
        int len=in.read(data);

        //打印浏览器发来的请求头
        System.out.println(new String(data));


        //制作响应报文
        StringBuffer response = new StringBuffer();

        //响应状态
        response.append("HTTP/1.1 200 OK\r\n");

        //响应头
        response.append("Content-type:text/html\r\n\r\n");

        //要返回的内容(当前时间)
        response.append("1213213232CurrentTime: ").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));


        //获取客户端的输出流
        OutputStream out=client.getOutputStream();

        //将以上内容写入
        out.write(response.toString().getBytes());

        //关闭客户端和服务端的流和Socket
        out.close();
        in.close();
        client.close();
        server.close();

    }
}