Spring Security REST登录

问题描述 投票:0回答:2

我对使用Spring Security登录REST API有疑问。对于Spring Security提供的默认登录窗口的登录工作正在使用数据库进行身份验证,我不知道如何进行自己的登录。我知道如何替换我自己的表单,但我应该在哪里发送数据?我应该张贴一些地址吗?我用用户名和密码制作了基本表格。

spring rest api security login
2个回答
0
投票

尝试这个,它可能会帮助你......至少要了解你所缺少的东西。此代码不保证100%工作,有些部分是故意错过的(错误处理及其格式,加载用户,一些检查,Session API)。

基本的想法是你必须注册一个过滤器(对所有安全的身份验证请求请求做出反应)和一个稍后将能够加载authonticated用户并为你创建安全上下文的提供者(例如,你知道每个请求都是按线程处理的,这个用户可以通过SecurityContextHolder / ThreadLocal获得。

您需要创建一个单独的控制器来处理创建用户会话(即登录/授权)的初始情况。此API的响应必须包含一些会话的GUID,以便稍后将其用作标题的值:Authentication: Bearer <value>

一些规格:https://tools.ietf.org/html/rfc6750

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)//optional
@Import(RestSecurityConfig.TokenAuthenticationProvider.class)// one of the way to create spring bean
public class RestSecurityConfig extends WebSecurityConfigurerAdapter {
    private static final RequestMatcher PUBLIC_URLS = new OrRequestMatcher(
            new AntPathRequestMatcher("/actuator/*"),
            new AntPathRequestMatcher("/some_api_to_login", POST), // this must be public
    );
    private static final RequestMatcher PROTECTED_URLS = new NegatedRequestMatcher(PUBLIC_URLS);

    // better to move it out as a separate class
    public static class TokenAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
        @Override
        public boolean supports(Class<?> authentication) {
            return MyAuthenticationToken.class.isAssignableFrom(authentication);
        }

        @Override
        protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {

        }

        @Override
        protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
            return null; // service/dao.loadUser
        }
    }

    public static class TokenAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
        public TokenAuthenticationFilter(RequestMatcher requiresAuthenticationRequestMatcher) {
            super(requiresAuthenticationRequestMatcher);
        }

        @Override
        public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
            Authentication auth = new MyAuthenticationToken(request.getHeader("Authentication"));
            return getAuthenticationManager().authenticate(auth);
        }
    }

    @Autowired
    TokenAuthenticationProvider authenticationProvider;

    @Override
    protected void configure(final AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(authenticationProvider);
    }

    @Override
    public void configure(final WebSecurity web) {
        web.ignoring().requestMatchers(PUBLIC_URLS);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // maybe some of the tuning you might not need
        http
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .exceptionHandling()
                .defaultAuthenticationEntryPointFor(new Http403ForbiddenEntryPoint(), PROTECTED_URLS).and()
                .authorizeRequests().anyRequest().authenticated().and()
                .cors().and()
                .anonymous().disable()
                .rememberMe().disable()
                .csrf().disable()
                .formLogin().disable()
                .httpBasic().disable()
                .logout().disable();

        // it's important
        http.addFilterBefore(tokenAuthenticationFilter(), AnonymousAuthenticationFilter.class);
    }

    @Bean
    AbstractAuthenticationProcessingFilter tokenAuthenticationFilter() throws Exception {
        final AbstractAuthenticationProcessingFilter filter = new TokenAuthenticationFilter(PROTECTED_URLS);
        filter.setAuthenticationManager(authenticationManager());
        filter.setAuthenticationSuccessHandler(successHandler());
        // maybe error handling to provide some custom response?
        return filter;
    }

    // it's critically important to register your filter properly in spring context
    /** Disable Spring boot automatic filter registration. */
    @Bean
    FilterRegistrationBean disableRegistrationForAuthenticationFilter(final TokenAuthenticationFilter filter) {
        final FilterRegistrationBean registration = new FilterRegistrationBean(filter);
        registration.setEnabled(false);
        return registration;
    }

    // this one also is critically important to avoid redirection
    @Bean
    SimpleUrlAuthenticationSuccessHandler successHandler() {
        final SimpleUrlAuthenticationSuccessHandler successHandler = new SimpleUrlAuthenticationSuccessHandler();
        successHandler.setRedirectStrategy(new NoRedirectStrategy());
        return successHandler;
    }

}

0
投票

您可以将用户名和密码存储在数据库中,您可以使用它来登录用户。您可以创建自己的类,该类扩展WebSecurityConfigurerAdapter并覆盖您需要修改的方法:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    DataSource dataSource;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth.jdbcAuthentication()
            .dataSource(dataSource)
    }
}

但是在搜索用户名和密码时,可以使用不同的Spring Security默认数据库查询,这样您就可以创建好的数据库模式:

public static final String DEF_USERS_BY_USERNAME_QUERY =
"select username,password,enabled " +
"from users " +
"where username = ?";
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY =
"select username,authority " +
"from authorities " +
"where username = ?";
public static final String DEF_GROUP_AUTHORITIES_BY_USERNAME_QUERY =
"select g.id, g.group_name, ga.authority " +
"from groups g, group_members gm, group_authorities ga " +
"where gm.username = ? " +
"and g.id = ga.group_id " +
"and g.id = gm.group_id";

但您也可以使用Spring方法指定您自己的数据库查询:

auth
.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery(
"select username, password, enabled from Users " +
"where username=?")

您应该将数据发布到您创建的某个服务,该服务将存储用户并传递给数据库。

© www.soinside.com 2019 - 2024. All rights reserved.