Learn Spring BOOT, Spring cloud Eureka and security

Source: Internet
Author: User

Interesting, tomorrow to the YANGPU register a spring cloud salon,

Take a closer look at hahaha today.

Eureka Service side:

Eurekaapplication.java

 PackageCom.packtpub.Eureka;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.boot.builder.SpringApplicationBuilder;Importorg.springframework.cloud.netflix.eureka.server.enableeurekaserver;@ Enableeurekaserver@springbootapplication Public classeurekaapplication { Public Static voidMain (string[] args) {//Springapplication.run (Eurekaapplication.class, args);        NewSpringapplicationbuilder (eurekaapplication.class). Web (true). Run (args); }}

Application.properties

server.port=1111eureka.client.register-with-eureka=falseEureka.client.fetch- registry=falseeureka.client.serviceUrl.defaultZone=http://localhost:${ server.port}/eureka/

Eureka Client:

 Packagecom.packtpub.EurekaClient;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.boot.builder.SpringApplicationBuilder;Importorg.springframework.cloud.client.discovery.enablediscoveryclient;@ Enablediscoveryclient@springbootapplication Public classeurekaclientapplication { Public Static voidMain (string[] args) {//Springapplication.run (Eurekaclientapplication.class, args);        NewSpringapplicationbuilder (eurekaclientapplication.class). Web (true). Run (args); }}

Computercontroller.java

 Packagecom.packtpub.EurekaClient;ImportOrg.apache.log4j.Logger;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.cloud.client.ServiceInstance;Importorg.springframework.cloud.client.discovery.DiscoveryClient;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classComputercontroller {Private FinalLogger Logger =Logger.getlogger (GetClass ()); @AutowiredPrivatediscoveryclient Client; @RequestMapping (Value= "/add", method=requestmethod.get) Publicinteger Add (@RequestParam integer A, @RequestParam integer b) {serviceinstance instance=client.getlocalserviceinstance (); Integer R= A +b; Logger.info ("/add, Host:" + instance.gethost () + ", service_id:" + instance.getserviceid () + ", Result:" +R); returnR; }}

Websecurityconfig.java

 Packagecom.packtpub.EurekaClient;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;Importorg.springframework.security.config.annotation.web.builders.HttpSecurity;Importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity;Importorg.springframework.security.config.annotation.web.configuration.websecurityconfigureradapter;@ Configuration@enablewebsecurity Public classWebsecurityconfigextendsWebsecurityconfigureradapter {@Overrideprotected voidConfigure (Httpsecurity http)throwsException {http. authorizerequests (). Antmatchers ("/", "Home"). Permitall (). Anyrequest (). authenticated (). and (). Formlogin () . LoginPage ("/login"). Permitall (). and (). Logout (). Permitall (); } @Autowired Public voidConfigureglobal (Authenticationmanagerbuilder auth)throwsException {auth. inmemoryauthentication (). Withuser ("User"). Password ("password"). Roles ("User").); }}

Hellocontroller.java

 Packagecom.packtpub.EurekaClient;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping; @Controller Public classHellocontroller {@RequestMapping ("/")     PublicString Index () {return"Index"; } @RequestMapping ("/hello")     PublicString Hello () {return"Hello"; } @RequestMapping ("/login")     PublicString Login () {return"Login"; }}

Application.properties

spring.application.name=compute-serviceserver.port=2222eureka.client.serviceUrl.defaultZone= http://localhost:1111/eureka/

Learn Spring BOOT, Spring cloud Eureka and security

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.