栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

一个应用程序中的Spring Security OAuth2和FormLogin

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一个应用程序中的Spring Security OAuth2和FormLogin

您需要为基于表单的登录和“资源服务器安全性”表单REST端点配置Web安全性

这是一个工作配置,该配置使用单点登录并单独部署了Authorization Server。

@Configuration@EnableOAuth2Sso@EnableWebSecurityprotected static class ResourceConfiguration extends WebSecurityConfigurerAdapter {    @Value("${sso.url}")    private String ssoUrl;    @Autowired    private  RedisConnectionFactory redisConnectionFactory;    @Bean    protected TokenStore tokenStore() {        return new RedisTokenStore(redisConnectionFactory);    }    @Bean    @Primary    protected ResourceServerTokenServices tokenServices() {        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();        defaultTokenServices.setTokenStore(tokenStore());        defaultTokenServices.setSupportRefreshToken(true);        return defaultTokenServices;    }    @Override    @Bean    public AuthenticationManager authenticationManagerBean() throws Exception {        OAuth2AuthenticationManager authenticationManager = new OAuth2AuthenticationManager();        authenticationManager.setTokenServices(tokenServices());        return authenticationManager;    }    @Override    protected void configure(HttpSecurity http) throws Exception {   http.requestMatchers()        .and().authorizeRequests() .antMatchers("/").permitAll() .antMatchers(HttpMethod.GET, "/static/**").permitAll() .antMatchers(HttpMethod.GET, "/profile/**").permitAll() .antMatchers(HttpMethod.GET, "/services/**").permitAll() .anyRequest().authenticated()        .and().logout()     .invalidateHttpSession(true)     .logoutSuccessUrl(ssoUrl+"/logout")     .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))     .deletecookies("JSESSIONID").invalidateHttpSession(true)     .permitAll();    }}@Configuration@EnableResourceServer@Order(1)protected static class ResourceServerConfig extends ResourceServerConfigurerAdapter {    @Override    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {        resources.resourceId("resource-id");    }    @Override    public void configure(HttpSecurity http) throws Exception {        http.requestMatcher(new OAuthRequestedMatcher()) .authorizeRequests().anyRequest().fullyAuthenticated();    }}private static class OAuthRequestedMatcher implements RequestMatcher {    public boolean matches(HttpServletRequest request) {        String auth = request.getHeader("Authorization");        boolean haveOauth2Token = (auth != null) && auth.startsWith("Bearer");        boolean haveAccessToken = request.getParameter("access_token")!=null;        return haveOauth2Token || haveAccessToken;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647688.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号