4.3.7 登录url放行
认证服务默认都要校验用户的身份信息,这里需要将登录url放行
在WebSecurityConfig类中重写 configure(WebSecurity web)方法,如下:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/userlogin");
}
4.3.8 测试认证接口
使用postman测试:
Post请求:http://localhost:40400/auth/userlogin
{
"success": true,
"code": 10000,
"message": "操作成功!",
"token": "b0192098-010b-4633-91ad-3bbbbea0455e"
}
所以测试时候,用户名填写itcast,密码123,填写其他密码都不对
4.3.9 测试写入Cookie
cookie最终会写到xuecheng.com域名下,可通过nginx代理进行认证,测试cookie是否写成功。
1、配置nginx代理
在ucenter.xuecheng.com下配置代理路径
#认证
location ^~ /openapi/auth/ {
proxy_pass http://auth_server_pool/auth/;
}
添加:
#认证服务
upstream auth_server_pool{
server 127.0.0.1:40400 weight=10;
}
2、请求:http://ucenter.xuecheng.com/openapi/auth/userlogin
观察cookie写入结果
ngnx完整代码
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#cms页面预览
upstream cms_server_pool{
server 127.0.0.1:31001 weight=10;
}
#静态资源服务
upstream static_server_pool{
server 127.0.0.1:91 weight=10;
}
#前端动态门户
upstream dynamic_portal_server_pool{
server 127.0.0.1:10000 weight=10;
}
#后台搜索(公开api)
upstream search_server_pool{
server 127.0.0.1:40100 weight=10;
}
#媒体服务
upstream video_server_pool{
server 127.0.0.1:90 weight=10;
}
#前端ucenter
upstream ucenter_server_pool{
#server 127.0.0.1:7081 weight=10;
server 127.0.0.1:13000 weight=10;
}
#学习服务
upstream learning_server_pool{
server 127.0.0.1:40600 weight=10;
}
#认证服务
upstream auth_server_pool{
server 127.0.0.1:40400 weight=10;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#门户的顶级域名,门户的主站
server {
listen 80;
server_name www.xuecheng.com;
ssi on;
ssi_silent_errors on;
location / {
alias E:/java_www/xcEduUI05/;
index index.html index.htm;
}
location /static/img/ {
alias E:/java_www/xcEduUI05/img/;
}
location /static/css/ {
alias E:/java_www/xcEduUI05/css/;
}
location /static/js/ {
alias E:/java_www/xcEduUI05/js/;
}
location /static/plugins/ {
alias E:/java_www/xcEduUI05/plugins/;
add_header Access-Control‐Allow‐Origin http://ucenter.xuecheng.com;
#add_header Access‐Control‐Allow‐Credentials true;
#add_header Access‐Control‐Allow‐Methods GET;
}
location /plugins/ {
alias E:/java_www/xcEduUI05/plugins/;
add_header Access-Control‐Allow‐Origin http://ucenter.xuecheng.com;
#add_header Access‐Control‐Allow‐Credentials true;
#add_header Access‐Control‐Allow‐Methods GET;
}
#页面预览
location /cms/preview/ {
proxy_pass http://cms_server_pool/cms/preview/;
}
location /static/company/ {
proxy_pass http://static_server_pool;
}
location /static/teacher/ {
proxy_pass http://static_server_pool;
}
location /static/stat/ {
proxy_pass http://static_server_pool;
}
location /course/detail/ {
proxy_pass http://static_server_pool;
}
#前端门户课程搜索
location ^~ /course/search {
proxy_pass http://dynamic_portal_server_pool;
}
#后端搜索服务
location /openapi/search/ {
proxy_pass http://search_server_pool/search/;
}
#分类信息
location /static/category/ {
proxy_pass http://static_server_pool;
}
#开发环境webpack定时加载此文件
location ^~ /__webpack_hmr {
proxy_pass http://dynamic_portal_server_pool/__webpack_hmr;
}
#开发环境nuxt访问_nuxt
location ^~ /_nuxt/ {
proxy_pass http://dynamic_portal_server_pool/_nuxt/;
}
}
#学成网媒体服务
server {
listen 90;
server_name localhost;
#视频目录
location /video/ {
alias E:/java_www/video/;
}
}
#学成网静态资源
server {
listen 91;
server_name localhost;
#公司信息
location /static/company/ {
alias E:/java_www/xcEduUI05/static/company/;
}
#老师信息
location /static/teacher/ {
alias E:/java_www/xcEduUI05/static/teacher/;
}
#统计信息
location /static/stat/ {
alias E:/java_www/xcEduUI05/static/stat/;
}
location /course/detail/ {
alias E:/java_www/xcEduUI05/course/detail/;
}
location /static/category/ {
alias E:/java_www/xcEduUI05/static/category/;
}
}
#学成网媒体服务代理
map $http_origin $origin_list{
default http://www.xuecheng.com;
"~http://www.xuecheng.com" http://www.xuecheng.com;
"~http://ucenter.xuecheng.com" http://ucenter.xuecheng.com;
}
#学成网媒体服务代理
server {
listen 80;
server_name video.xuecheng.com;
location /video {
proxy_pass http://video_server_pool;
#add_header Access-Control‐Allow‐Origin http://www.xuecheng.com;
add_header Access‐Control‐Allow‐Origin $origin_list;
#add_header Access‐Control‐Allow‐Origin *;
#add_header Access‐Control‐Allow‐Credentials true;
#add_header Access‐Control‐Allow‐Methods GET;
}
}
#学成网用户中心
server {
listen 80;
server_name ucenter.xuecheng.com;
#个人中心
location / {
proxy_pass http://ucenter_server_pool;
}
#后端搜索服务
location /openapi/search/ {
proxy_pass http://search_server_pool/search/;
}
#学习服务
location ^~ /api/learning/ {
proxy_pass http://learning_server_pool/learning/;
}
#认证
location ^~ /openapi/auth/ {
proxy_pass http://auth_server_pool/auth/;
}
}
}