【问题标题】:Spring Boot security for rest用于休息的 Spring Boot 安全性
【发布时间】:2017-09-16 13:47:20
【问题描述】:

我知道这方面有很多主题,但有什么办法可以修改正常的 spring 安全性以使用 json 对象。

@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();
    }

}

这是我的配置文件,它可以在没有休息的情况下完美运行,但我的问题只是想让登录页面在休息的情况下工作,仅此而已。如果它是这样配置的,我的登录是自动完成的,我什至不能在我的控制器中设置断点。它可以工作,但我想让它在休息时工作。

【问题讨论】:

    标签: rest security spring-boot


    【解决方案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。

    【讨论】:

    • 你能再具体一点吗
    • 我刚刚更新了我的答案,如果您有任何疑问,请查看并告诉我。
    猜你喜欢
    • 1970-01-01
    • 2017-07-07
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    相关资源
    最近更新 更多