Spring Security Oauth2.0认证授权

11.Spring Security快速上手-授权功能

2020-03-19  本文已影响0人  LANSHENGYANG

授权

@RestController
public class LoginController {
    @RequestMapping(value = "/r/r1", produces = "text/plain;charset=utf-8")
    public String r1(HttpSession session) {
        return "访问资源r1";
    }

    @RequestMapping(value = "/r/r2", produces = "text/plain;charset=utf-8")
    public String r2(HttpSession session) {
        return "访问资源r2";
    }

}
//安全拦截机制(最重要)
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/r/r1").hasAnyAuthority("p1")
            .antMatchers("/r/r2").hasAnyAuthority("p2")
            .antMatchers("/r/**").authenticated()//所有/r/**的请求必须认证通过
            .anyRequest().permitAll()//除了/r/**,其他的请求可以访问
            .and()
            .formLogin()//允许表单登录
            .successForwardUrl("/login-success");//自定义登录成功的页面地址
}
上一篇下一篇

猜你喜欢

热点阅读