2017-09-16 48 views
1

我知道这里有很多话题,但是有什么方法可以修改普通的spring安全来处理json对象。休息时的Spring Boot安全

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) //za pre i post authorize v servisa 
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter 
{ 
    //Koi service shte polzvame 
    @Autowired 
    private UserService userService; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception 
    { 
     http.authorizeRequests() 
       .antMatchers("/", "/user/register", "/css/**", "/js/**").permitAll() 
       .antMatchers("/user/user").access("hasRole('USER') or hasRole('ADMIN')") 
       .antMatchers("/user/admin").hasRole("ADMIN") 
       .anyRequest().authenticated() 
       .and() 
       .formLogin().loginPage("/user/login").permitAll() 
       .usernameParameter("username") 
       .passwordParameter("password") 
       .and() 
       .rememberMe().rememberMeCookieName("RememberMeFromLecture") 
       .rememberMeParameter("remember") 
       .key("golqmaTaina") 
       .and() 
       .logout().logoutSuccessUrl("/user/login?logout").logoutRequestMatcher(new AntPathRequestMatcher("/signout")).permitAll() 
       .and() 
       .exceptionHandling().accessDeniedPage("/user/unauthorized") 
       .and().csrf().disable(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception 
    { 
     auth.userDetailsService(this.userService).passwordEncoder(getBCryptPasswordEncoder()); 
    } 

    @Bean 
    public BCryptPasswordEncoder getBCryptPasswordEncoder() 
    { 
     return new BCryptPasswordEncoder(); 
    } 

} 

这是我的配置文件,它的工作原理完全没有休息,但我的问题是,只是想登录页面休息这就是全部的工作。如果它是这样的配置,我的登录自动完成,我甚至不能在我的控制器中设置一个断点。它的工作原理,但我想让它与休息一起工作。

回答

1

我创建了一个示例应用程序(https://github.com/manishsingh27/TokenBasedAuth),它基于REST进行身份验证。 客户端应用程序基于AngularJS,它有登录页面,文件在这里 - https://github.com/manishsingh27/TokenBasedAuth/tree/main/authz/src/main/resources/static。 此处存在REST API - https://github.com/manishsingh27/TokenBasedAuth/blob/main/authz/src/main/java/com/adms/authz/self/user/controller/UsersController.java。 配置文件在这里 - https://github.com/manishsingh27/TokenBasedAuth/blob/main/authz/src/main/java/com/adms/authz/config/SecurityConfiguration.java
您需要使用@EnableResourceServer注释来保护Rest API。

+0

你可以稍微更具体然后请 – Alexander

+0

我刚刚更新了我的答案,请检查并让我知道你是否有任何疑问。 – ManishSingh