web开发中的安全问题解决

常用:SpringSecurity

身份验证+权限控制 框架

SpringSecurity授权认证代码模版

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
​
    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/page1/**").hasRole("A")
                .antMatchers("/page2/**").hasRole("B")
                .antMatchers("/page3/**").hasRole("C");
​
        // 无权限会回到登录页面
        http.formLogin().loginPage("/login");
        
        // 开启注销功能
        http.csrf().disable(); 
        http.logout().logoutSuccessUrl("/");
​
        // 开启记住我功能
        http.rememberMe().rememberMeParameter("remember");
    }
​
    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //这里拓展需要连数据库
        //密码需要加密,不然会失败
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("Qiddo").password(new BCryptPasswordEncoder().encode("123456")).roles("A", "B")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("A", "B", "C")
                .and()
                .withUser("man").password(new BCryptPasswordEncoder().encode("123456")).roles("A");
    }
}

注:代码中认证部分没有用到数据库去判别身份,直接自己在后端创建的,具体的复杂代码还得看官方手册注解去实现更多的认证功能,授权同理,很多都是自定义的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值