会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132834个问题
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask视图基础和URL 16471楼
软件测试 全系列/预科阶段:测试环境和操作系统/软件测试和测试环境 16474楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 16476楼
Python 全系列/第七阶段:网页编程基础/CSS 样式 16477楼

mybatisDemo.zip

image.png

image.png

老师,找不到错误,帮我看下

JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 16478楼


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

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(旧) 16479楼

image.png

JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 16481楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 16483楼
大数据全系列/第一阶段:Linux 操作系统/Linux操作系统概述与安装 16485楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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