需要Spring Security Java Configuration帮助

时间:2017-01-12 02:10:22

标签: spring security

我是Spring Security的新手。尝试使用java配置的简单演示。非常感谢您解决我的疑问。这是我试过的演示 SecurityWebApplicationInitializer .java

 package initializer;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
public class SecurityWebApplicationInitializer 
extends AbstractSecurityWebApplicationInitializer{
        public SecurityWebApplicationInitializer() {
        super(config.SecurityConfig.class);
        System.out.println("sec web app init");
    }
}

WebApplicationInitializer.java

package initializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebApplicationInitializer implements org.springframework.web.WebApplicationInitializer {
     public void onStartup(ServletContext container) throws ServletException {
            System.out.println("in web app init");
            AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
            ctx.register(config.MVCConfig.class);
            ctx.setServletContext(container);
            ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
            servlet.setLoadOnStartup(1);
           servlet.addMapping("/*");
            }
 }

SecurityConfig.java

package config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.*;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig  extends WebSecurityConfigurerAdapter{
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        System.out.println("in sec config-global");
        auth
            .inMemoryAuthentication()
                .withUser("ram").password("ram123").roles("ADMIN");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        System.out.println("in sec config- configure");
         http.authorizeRequests()
            .antMatchers("/*").permitAll()
            .antMatchers("/admin**").hasRole("ADMIN")
            .and().httpBasic();
    }
}

MVCConfig.java

package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="controller")
public class MVCConfig {
     @Bean(name = "viewResolver")
        public InternalResourceViewResolver getViewResolver() {
         System.out.println("inside view resolver- MVC Config");
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setPrefix("/WEB-INF/jsp/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }
}

MVCController.java

package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MVCController {
     @RequestMapping(value="/*", method = RequestMethod.GET)
        public ModelAndView visitHome() {
         System.out.println("in controller-> vissit home");
            return new ModelAndView("index");
        }

        @RequestMapping(value="/admin", method = RequestMethod.GET)
        public ModelAndView visitAdmin() {
             System.out.println("in controller-> vissit admin");
            ModelAndView model = new ModelAndView("admin");
            model.addObject("title", "Admministrator Control Panel");
            model.addObject("message", "This page demonstrates how to use Spring security.");

            return model;
        }
}

/WEB-INF/jsp/index.jsp



 <div align="center">
        <h1>Spring Security Basic Demo (Java Config)</h1>
        <a href="/admin">Go to Administrator Page</a>
    </div>
&#13;
&#13;
&#13;

/WEB-INF/jsp/admin.jsp

&#13;
&#13;
<div align="center">
        <h1>${title}</h1>
        <h2>${message}</h2>
        <c:if test="${pageContext.request.userPrincipal.name != null}">
            <h2>Welcome : ${pageContext.request.userPrincipal.name} |
                <a href="/<c:url value="/logout" />" > Logout</a></h2> 
    </c:if>              
    </div>
&#13;
&#13;
&#13;

当我运行此应用程序时,我收到以下错误消息:

    " No mapping found for HTTP request with URI [/FirstSecurityDemo/WEB-INF/jsp/index.jsp] in DispatcherServlet with name 'dispatcher'"

我了解安全配置未获得应用。有关如何解决此问题的任何想法?

0 个答案:

没有答案