使用Java配置类配置SpringSecurity

终于要开始配置SpringSecurityla,鸡冻鸡冻!!

第一步、先找个漂亮的登录页面修改修改

比如我找的这个:https://down.shafish.cn/web/disanfanglogin.zip,把它解压放在你的项目中,顺便把index.html改为login.html

厚颜无耻
厚颜无耻+

  1. 配置login.html form标签内容

    // 加上action和method
    <form class="login100-form validate-form" action="/doLogin" method="post">
    ...
    </form>
  2. 修改用户名和密码的input.name属性值为username password

    // username和password
    <input class="input100" type="text" name="username" placeholder="请输入用户名" autocomplete="off">
    <input class="input100" type="password" name="password" placeholder="请输入密码">
  3. 登录按钮加上提交type

    // submit
    <button class="login100-form-btn" type="submit">登 录</button>

    OK

第二步、开始配置SpringSecurity

首先继承WebSecurityConfigurerAdapter这个抽象类,这个类可以让你方便+简单地配置出安全的项目。

@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
    // 密码加盐算法,每次都会加不同的盐,保证密码安全可靠
    // BCryptPasswordEncoder提供了密码匹配方法,100%避免了密码明文存储
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    /**
     * 配置用户认证信息,会覆盖application.yml中的设置,所以把上一篇在application.yml
     * 中设置的东西删掉就可以了,这里要做密码加密。安全感大大地提高了呢
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("shafish")
                .password(passwordEncoder().encode("123456"))
                .roles("ADMIN");// 用户的角色,随便写,也可以不写
    }
    /**
     * 放行web静态资源,浏览器要用来渲染页面,就没必要拦截
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**","/css/**",
                "/images/**","/vendor/**","/fonts/**");
    }
    /**
     * 认证和授权方面配置
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                // 设置登录页面
                .formLogin().loginPage("/login.html")
                // 配置登录接口,对接form表单的action请求
                .loginProcessingUrl("/doLogin")
                // 登录成功后跳转到/loginSuccess接口
                // defaultSuccessUrl可以记录用户登录前的请求路径,
                // 登录成功后就不会跳转到默认登录成功页面
                .defaultSuccessUrl("/loginSuccess")
                // 对所有人开放上面的接口
                .permitAll()
                .and()
                // 配置注销操作,注销了就跳转到登录页
                .logout().logoutSuccessUrl("/login.html")
                .and()
                // 所有其他请求都需要登录权限
                .authorizeRequests().anyRequest()authenticated()
                .and()
                // 现在先禁用跨域
                .csrf().disable();
    }
}

/doLogin接口进行账号密码认证,现在暂时交给框架处理,我们就只需要定义就好。那就只剩下/loginSuccess,来吧

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }
    // 简单明了
    @RequestMapping("/loginSuccess")
    public String loginSuccess() {
        return "登录成功";
    }
}

启动项目!

  • 访问http://localhost:8080/login.html输入shafish 123456就可以看到输出 ->登录成功
  • 我们还开启了注销接口,登录成功后试试访问http://localhost:8080/logout

撒花

ok,在这一篇文章里,你学会了在项目中写一个漂亮的html登录页面,也能够用SpringSecurity处理登录接口la,不错哟☜(゚ヮ゚☜)

温馨提示:例子可以在 https://gitee.com/shafish/demo/tree/springsecurity1/ 找到

标签: Springsecurity

添加新评论