会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132945个问题


我照着敲的,访问时候弹出这个

image.png

package com.security.handle;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author  liufupeng
 * @date  2021/5/11
 */
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {

        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
        httpServletResponse.setContentType("application/json:charset=utf-8");
        PrintWriter writer = httpServletResponse.getWriter();
        writer.println("{\"code\":\"403\",\"msg\":\"无权限\"}");
        writer.flush();
        writer.close();
    }


}
package com.security.config;

import com.security.handle.MyAccessDeniedHandler;
import com.security.handle.MyAuthenticationFailHandler;
import com.security.handle.MyAuthenticationSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author liufupeng
 * @date 2021/5/8
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyAccessDeniedHandler myAccessDeniedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        // 表单认证
        http.formLogin()
                .loginProcessingUrl("/login") // 当发现/login时认为是登陆,需要执行UserDetailsServiceImpl
//                .successForwardUrl("/toMain") // 登陆成功 此处为post请求
//                .failureForwardUrl("/fail")
                .usernameParameter("username") //自定义username字段
                .passwordParameter("password")
                .successHandler(new MyAuthenticationSuccessHandler("/toMain"))
                .failureHandler(new MyAuthenticationFailHandler("/fail.html"))
                .loginPage("/login.html");

        //  url 拦截
        http.authorizeRequests()
                .antMatchers("/login.html", "/fail.html").permitAll() // 登陆不需要被认证
//                .antMatchers("/main1.html").hasAuthority("admin")
                .antMatchers("/main1.html").hasIpAddress("127.0.0.1")
                .anyRequest().authenticated();

        http.csrf().disable();

        http.exceptionHandling()
                .accessDeniedHandler(myAccessDeniedHandler);
    }

    @Bean
    public PasswordEncoder getPe() {
        return new BCryptPasswordEncoder();
    }
}


JAVA 全系列/第九阶段:权限控制与安全认证/Spring Security(旧) 21572楼
Python 全系列/第一阶段:Python入门/面向对象 21574楼
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 21575楼

老师你好,34行的wait()语句他可以自己单独调用的吗,还是跟23行里面的sleep(6)有关联的,程序开始运行的时候调用了ti,t2线程打印 车开走了和等车 这2句话,一起等待了6秒之后才变为True,有点没搞懂

from threading import Thread
from threading import Event
from time import sleep
'''
1.Event()可以创建一个事件管理标志,该标志(event)默认为False。
2.event.wait(timeout=None):调用该方法的线程会被阻塞,如果设置了,
  timeout 参数,超时后,线程会停止阻塞继续执行;
'''
def car():
    #global count
    num=0
    while True:
        if even.is_set(): #判断event的标值是否为True
            print('车已经到站,可以上车')
            sleep(1)
            #count=count+1
            num=num+1
            if num % 5  == 0:
                even.clear() #将event设置为False

        else:
            print('车开走了...')
            sleep(6)
            #count=1
            even.set()  #将event设置为True

def person():
    while True:
        if even.is_set():
            print('上车')
            sleep(1)
        else:
            print('等车')
            even.wait()
            #sleep(1)

if __name__=='__main__':
    even=Event()
    #count=1
    t1=Thread(target=car)
    t2=Thread(target=person)
    t1.start()
    t2.start()


Python 全系列/第三阶段:Python 网络与并发编程/并发编程 21576楼
人工智能/第十阶段:机器学习-Kaggle竞赛实战/网页分类案例 21577楼
Python 全系列/第一阶段:Python入门/面向对象 21578楼

#测试grid布局管理器
from tkinter import *

class Application(Frame):

    def __init__(self,master=None):  #master默认是None,实例化时传入参数root替换None
        super().__init__(master)
        self.master=master
        self.pack()
        self.createWidget()

    def createWidget(self):   #定义框内容以及属性

        self.l0=Label(self,text="用户名")
        #grid(row=0,column=0)--在0行0列放置
        self.l0.grid(row=0,column=0)

        #self.v=StringVar()
        #self.e0 = Entry(self, textvariable=self.v)
        #self.e0.grid(row=0, column=1)
        Entry(self).grid(row=0, column=1)

        self.l1 = Label(self, text="密码").grid(row=1, column=0)

        #self.v1 = StringVar()
        #self.e1 = Entry(self, textvariable=self.v1,show='*')
        #self.e1.grid(row=1, column=1)
        Entry(self,show='*').grid(row=1, column=1)

        self.l2 = Label(self, text="用户名为手机号").grid(row=0, column=2)

        Button(self,text="登录").grid(row=2, column=1,sticky=EW)  #EW,沿东西方向都有
        Button(self, text="取消").grid(row=2, column=2, sticky=E)


if __name__=="__main__":
    root=Tk()                        #创建框大小、位置以及标题
    app=Application(master=root)
    root.title("grid")
    root.geometry("400x120+450+200")
    root.mainloop()

老师请问以下红框和绿框实现的功能是一样的,这个两个写法有什么区别呢?那是说明不需要设置变量StringVar()也可以吗?还是说在特定情况下才可以?

image.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 21579楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 21580楼

老师你好,你能帮我看看以下程序为什么会出现这种错误吗?

<label for="username">
    <span>用户名:</span><input type="text"  id="username"  class="username"/>
</label><br/>
    <label for="password">
    <span>密码:</span><input type="text"  id="password"  class="password"/>
</label><br/>
    <button>登陆</button>
 <script>
        var usernameInput = document.querySelector('.username');
        var passwordInput = document.querySelector('.password');
        var btn = document.querySelector('button');

        btn.onclick = function() {
            //兼容性问题,考虑兼容IE使用 ActiveXObject()  非IE使用XMLHttpRequest()
            //        xhr = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("");
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        console.log(JSON.parse(xhr.responseText));
                    }
                }
            };

            xhr.open('post', '03post请求.php', true);

            //构建post表单数据,使用FormData类构建数据
            var datas = new FormData();
            datas.append('uname', usernameInput.value);
            datas.append('upass', passwordInput.value);
            xhr.send(datas);
        }
    </script>
<?php
     $success = array('msg'=>"ok","info"=>$POST);
    echo json_encode($success);
?>

图片.png


图片.png






WEB前端全系列/第六阶段:Http服务与Ajax模块(旧)/Http服务与Ajax编程 21581楼

from multiprocessing import Process
from time import sleep

class Myprocess(Process):
    def __init__(self,name):#这个name就是类里面的参数
        Process.__init__(self)
        self.name=name
    def run(self):#这个self就不需要再写个namae了 因为上面有了
        print(f"Process:{self.name}start")
        
p1=Myprocess('pcl')

p1.start()

    老师 请问报错里 为什么会出现几百行的错误 我的代码都没有那么多行


C:\Users\pcl\venv\Scripts\python.exe "C:/Users/pcl/.1aPython all exercise/线程与进程/进程实现/进程类包装.py"

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 116, in spawn_main

    exitcode = _main(fd, parent_sentinel)

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 125, in _main

    prepare(preparation_data)

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 236, in prepare

    _fixup_main_from_path(data['init_main_from_path'])

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path

    main_content = runpy.run_path(main_path,

  File "C:\Users\pcl\lib\runpy.py", line 268, in run_path

    return _run_module_code(code, init_globals, run_name,

  File "C:\Users\pcl\lib\runpy.py", line 97, in _run_module_code

    _run_code(code, mod_globals, init_globals,

  File "C:\Users\pcl\lib\runpy.py", line 87, in _run_code

    exec(code, run_globals)

  File "C:\Users\pcl\.1aPython all exercise\线程与进程\进程实现\进程类包装.py", line 13, in <module>

    p1.start()

  File "C:\Users\pcl\lib\multiprocessing\process.py", line 121, in start

    self._popen = self._Popen(self)

  File "C:\Users\pcl\lib\multiprocessing\context.py", line 224, in _Popen

    return _default_context.get_context().Process._Popen(process_obj)

  File "C:\Users\pcl\lib\multiprocessing\context.py", line 327, in _Popen

    return Popen(process_obj)

  File "C:\Users\pcl\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__

    prep_data = spawn.get_preparation_data(process_obj._name)

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 154, in get_preparation_data

    _check_not_importing_main()

  File "C:\Users\pcl\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main

    raise RuntimeError('''

RuntimeError: 

        An attempt has been made to start a new process before the

        current process has finished its bootstrapping phase.


        This probably means that you are not using fork to start your

        child processes and you have forgotten to use the proper idiom

        in the main module:


            if __name__ == '__main__':

                freeze_support()

                ...


        The "freeze_support()" line can be omitted if the program

        is not going to be frozen to produce an executable.


Process finished with exit code 0


Python 全系列/第三阶段:Python 网络与并发编程/并发编程 21582楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 21583楼
Python 全系列/第十四阶段:Python 爬虫开发/爬虫反反爬- 21584楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 21585楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637