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

Spring Security:用于API的JWT令牌和用于Web的会话

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

Spring Security:用于API的JWT令牌和用于Web的会话

经过6小时的搜索,以下是解决方案:https :
//docs.spring.io/spring-
security/site/docs/current/reference/htmlsingle/#multiple-
httpsecurity

编辑:这是我的方法:

@EnableWebSecuritypublic class MultiHttpSecurityConfig {    @Autowired    private UserDetailsService userDetailsService;    @Bean    public PasswordEnprer passwordEnprer() {        return new BCryptPasswordEnprer(12);    }    @Configuration    @Order(1)    public class ApiSecurityAdapter extends WebSecurityConfigurerAdapter {        private TokenProvider tokenProvider;        public ApiSecurityAdapter(TokenProvider tokenProvider) { this.tokenProvider = tokenProvider;        }        @Override        protected void configure(HttpSecurity http) throws Exception { http.antMatcher("/api/**") //<= Security only available for /api/**     .authorizeRequests()         .antMatchers("/api/register").permitAll()         .antMatchers("/api/login").permitAll()         .antMatchers("/api/public").permitAll()         .antMatchers("/api/lost").permitAll()         .anyRequest().authenticated()     .and()         .apply(new JWTConfigurer(this.tokenProvider))     .and()         .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);        }    }    @Configuration    public class WebSecurityAdapter extends WebSecurityConfigurerAdapter {        @Override        protected void configure(HttpSecurity http) throws Exception { http // <= Security available for others (not /api/)     .authorizeRequests()         .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")         .antMatchers("/").permitAll()         .antMatchers("/login").permitAll()         .antMatchers("/resources/**").permitAll()         .anyRequest().authenticated()     .and()         .formLogin()  .loginPage("/login")      .usernameParameter("email")      .passwordParameter("password")      .defaultSuccessUrl("/central", false)      .failureForwardUrl("/login/fail")     .and()         .logout()  .invalidateHttpSession(true)  .logoutUrl("/logout")  .logoutSuccessUrl("/")     .and()         .csrf();        }    }}

希望这会有所帮助!



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

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

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