SpringBootAdminApplication.java 3.73 KB
Newer Older
Thomas Bosch committed
1
/*
2
 * Copyright 2014-2018 the original author or authors.
Thomas Bosch committed
3 4 5 6 7
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
Johannes Edmeier committed
8
 *     http://www.apache.org/licenses/LICENSE-2.0
Thomas Bosch committed
9 10 11 12 13 14 15
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
16

Thomas Bosch committed
17
package de.codecentric.boot.admin;
Thomas Bosch committed
18

19
import de.codecentric.boot.admin.server.config.AdminServerProperties;
20 21
import de.codecentric.boot.admin.server.config.EnableAdminServer;

Thomas Bosch committed
22 23
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
24
import org.springframework.context.annotation.Bean;
Thomas Bosch committed
25
import org.springframework.context.annotation.Configuration;
26 27 28 29
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
30 31
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
32
import com.hazelcast.config.InMemoryFormat;
33
import com.hazelcast.config.MapConfig;
Thomas Bosch committed
34

35
// tag::application-hazelcast[]
Thomas Bosch committed
36 37
@Configuration
@EnableAutoConfiguration
Dennis Schulte committed
38 39
@EnableAdminServer
public class SpringBootAdminApplication {
40 41
    @Bean
    public Config hazelcastConfig() {
42 43 44 45
        MapConfig mapConfig = new MapConfig("spring-boot-admin-event-store").setInMemoryFormat(InMemoryFormat.OBJECT)
                                                                            .setBackupCount(1)
                                                                            .setEvictionPolicy(EvictionPolicy.NONE);
        return new Config().setProperty("hazelcast.jmx", "true").addMapConfig(mapConfig);
46
    }
Thomas Bosch committed
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    @Profile("insecure")
    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().permitAll()//
                .and().csrf().disable();
        }
    }

    @Profile("secure")
    @Configuration
    public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
        private final String adminContextPath;

        public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
            this.adminContextPath = adminServerProperties.getContextPath();
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // @formatter:off
            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
            successHandler.setTargetUrlParameter("redirectTo");

            http.authorizeRequests()
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
            .logout().logoutUrl(adminContextPath + "/logout").and()
            .httpBasic().and()
            .csrf().disable();
            // @formatter:on
        }
    }

86 87 88
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminApplication.class, args);
    }
89

Thomas Bosch committed
90
}
91
// end::application-hazelcast[]