页面禁用F12或禁用右键查看源码

页面禁用F12或禁用右键查看源码

页面上线以后,有时候会有特殊的需求,比如不想让别人查看到页面的源码,对于传统的html、jsp页面,我们在页面内写的一些方法都会直接暴露出来,这就需要我们去想办法解决这个问题。


前言

下面就介绍两种思路去解决这个问题,本文以springboot框架为原型

1、利用HttpSecurity授权,赋予js、css、图片、文件等的权限,能让用户在没登录前无法查看

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
        		.antMatchers("/images/*.png").permitAll()
        		.antMatchers("/css/*.css").permitAll()
        	    .anyRequest().authenticated(); 
    }
}

2、直接在单独的页面去禁用F12或者鼠标右键,别忘记页面引入jquery.min.js文件,这样页面就无法F12或者右键查看源码了

<script type="text/javascript">
	//禁止右键
	$(document).bind("contextmenu", function () {
		 return false; 
	 });
	document.oncontextmenu = function () { 
		return false; 
	};
	document.onkeydown = function () {
    if (window.event && window.event.keyCode == 123) {
        event.keyCode = 0;
        event.returnValue = false;
        return false;
    }
}
</script>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值