Spring Security HTTP Basic for RESTFul and FormLogin (Cookies) for web - Annotations

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private AuthenticationProvider authenticationProvider;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
    }

    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                    .antMatcher("/api/**")
                    .authorizeRequests()
                        .anyRequest().hasAnyRole("ADMIN", "API")
                        .and()
                    .httpBasic();
        }
    }

    @Configuration
    @Order(2)
    public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/css/**", "/js/**", "/img/**", "/lib/**");
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable() //HTTP with Disable CSRF
                    .authorizeRequests() //Authorize Request Configuration
                        .antMatchers("/connect/**").permitAll()
                        .antMatchers("/", "/register").permitAll()
                        .antMatchers("/admin/**").hasRole("ADMIN")
                        .anyRequest().authenticated()
                        .and() //Login Form configuration for all others
                    .formLogin()
                        .loginPage("/login").permitAll()
                        .and() //Logout Form configuration
                    .logout().permitAll();
        }
    }
}

http://stackoverflow.com/questions/27774742/spring-security-http-basic-for-restful-and-formlogin-cookies-for-web-annotat/27833237#27833237

 

时间: 2024-09-19 14:15:06

Spring Security HTTP Basic for RESTFul and FormLogin (Cookies) for web - Annotations的相关文章

使用Spring Security和OAuth2实现RESTful服务安全认证

这篇教程是展示如何设置一个OAuth2服务来保护REST资源. 源代码下载github. (https://github.com/iainporter/oauth2-provider)你能下载这个源码就开始编写一个被OAuth方法保护的服务.该源码包含功能: * 用户注册和登录* Email验证* Password 丢失 采取的技术有以下: * OAuth2 Protocol * spring Security * Spring Integration * Spring Data * Jerse

spring security不登陆也可以任意访问

问题描述 项目中准备用spring security,根据网上搜索的资料做了一个demo,但是测试结果是不用登陆也能正常任意访问,请各位帮忙看下哪里有问题,谢谢.以下是配置文件:web.xml<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-

Spring boot +Spring Security + Thymeleaf 认证失败返回错误信息

  [Please make sure to select the branch corresponding to the version of Thymeleaf you are using] Status This is a thymeleaf extras module, not a part of the Thymeleaf core (and as such following its own versioning schema), but fully supported by the

Spring Security笔记:HTTP Basic 认证

在第一节 Spring Security笔记:Hello World 的基础上,只要把Spring-Security.xml里改一个位置 1 <http auto-config="true"> 2 <intercept-url pattern="/admin" access="ROLE_USER" /> 3 <http-basic /> 4 </http> 注意第三行,加上<http-basi

Spring Boot中集成Spring Security 专题

if语句中条件判断就是检查当前的url请求是否是logout-url的配置值,接下来,获取用户的authentication,并循环调用处理器链中各个处理器的logout()函数,前面在parse阶段说过,处理器链中有两个实例,处理会话的SecurityContextLogoutHandler及remember-me服务,我们来一一看看它们的logout函数实现: 2.1.0 SecurityContextLogoutHandler public void logout(HttpServletR

spring security之httpSecurity 专题

  37.5.2 Resolving the CsrfToken Spring Security provides CsrfTokenArgumentResolver which can automatically resolve the current CsrfToken for Spring MVC arguments. By using @EnableWebSecurity you will automatically have this added to your Spring MVC

Spring Security笔记:自定义Login/Logout Filter、AuthenticationProvider、AuthenticationToken

在前面的学习中,配置文件中的<http>...</http>都是采用的auto-config="true"这种自动配置模式,根据Spring Security文档的说明: ------------------ auto-config Automatically registers a login form, BASIC authentication, logout services. If set to "true", all of thes

Spring Security 4.0.0.RC2/3.2.6 发布

Spring Security 4.0.0.RC2 发布,此版本解决了大约 50 tickets. 主要改进: Support for enforcing Same Origin for WebSocket connections Refinements in WebSocket Configuration (SEC-2827 SEC-2833 SEC-2853 ) More intuitive HTTP Response Headers Configuration (SEC-2846) GA

Spring Security 入门详解(转)

1.Spring Security介绍 Spring Security是基于spring的应用程序提供声明式安全保护的安全性框架,它提供了完整的安全性解决方案,能够在web请求级别和方法调用级别 处理身份证验证和授权.它充分使用了依赖注入和面向切面的技术. Spring security主要是从两个方面解决安全性问题: web请求级别:使用servlet过滤器保护web请求并限制URL级别的访问 方法调用级别:使用Spring AOP保护方法调用,确保具有适当权限的用户采用访问安全保护的方法.