简体   繁体   中英

Spring Security Http status 406

I'm working on login with http basic, following this http://howtodoinjava.com/2013/04/16/login-form-based-spring-3-security-example/ and it works well, but some of my page give me http status 406 when I try to load them,I try some answer from similar cases but none of them works(maybe because I just implementing them as close as I can)Any suggestion? thanks

this is my requestController that still work fine

@Controller
@RequestMapping(value = "/request")
public class RequestController {

@Autowired
private UploadFileService uploadFileService;

@RequestMapping(value = "", method = RequestMethod.GET)
public ModelAndView index() {
    return new ModelAndView("page/request-view-table-req");
}

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(@RequestParam("file") MultipartFile file) {
    try {
        if (file.getOriginalFilename().endsWith("XLS") || file.getOriginalFilename().endsWith("xls")) {
            Excel excel = new Excel(file);
            excel.convertToUF();
            uploadFileService.add(excel.getListUF());
        }
        return "redirect:/";

    } catch (NullPointerException ex) {
        return "redirect:/";
    }
}
}

this is templateController that return http status 406

@Controller
@RequestMapping(value = "/template")
public class TemplateController {

@Autowired
private RequestTemplateService requestTemplateService;

@RequestMapping(value = "", method = RequestMethod.GET)
@ResponseBody
public ModelAndView templateView(Model model) {
    list_all_template(model);
    return new ModelAndView("page/template-add");
}

@RequestMapping(value = "/add", method = RequestMethod.GET)
@ResponseBody
public ModelAndView templateAdd(Model model) {
    list_all_template(model);
    return new ModelAndView("page/template-add");
}

@RequestMapping(value = "/add/new", method = RequestMethod.POST)
@ResponseBody
public String addTemplate(@ModelAttribute RequestTemplate rt, Model model) {
    requestTemplateService.add(rt);
    list_all_template(model);
    return "redirect:/template";
}

@RequestMapping(value="/delete", method = RequestMethod.GET)
@ResponseBody
public ModelAndView deleteSelected (@RequestParam(value = "nama", required = true) String nama, 
                                  Model model)
{
    delete_by_nama(nama);
    list_all_template(model);
    return new ModelAndView("page/template-add");
}

@RequestMapping(value="/update", method = RequestMethod.GET)
@ResponseBody
public ModelAndView updateSelected (@RequestParam(value = "nama", required = true) String nama,
                                    @RequestParam(value = "template") String template,
                                    Model model)
{
    requestTemplateService.updateByNama(nama, template);
    list_all_template(model);
    return new ModelAndView("page/template-view");
}

@RequestMapping(value="/view", method = RequestMethod.GET)
@ResponseBody
public ModelAndView viewSelected (@RequestParam(value = "nama", required = true) String nama,
                                  HttpServletRequest request, HttpServletResponse response, Model model)
{
    list_all_template(model);
    get_template_by_name(model, nama);
    return new ModelAndView("page/template-view");
}

public void delete_by_nama(String nama) {
    requestTemplateService.deleteByNama(nama);
}

public void get_template_by_name(Model model, String nama) {
    List<RequestTemplate> selectedTemplate = requestTemplateService.getByNama(nama);
    model.addAttribute("selectedTemplate", selectedTemplate);
}

public void list_all_template(Model model) {
    List<RequestTemplate> RequestTemplates = requestTemplateService.findAll();
    model.addAttribute("RequestTemplates", RequestTemplates);
}
}

this is my application-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
        xmlns:beans="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

<http auto-config="true"  use-expressions="true">
    <intercept-url pattern="/login" access="permitAll" />
    <intercept-url pattern="/logout" access="permitAll" />
    <intercept-url pattern="/accessdenied" access="permitAll" />
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    <form-login login-page="/login" default-target-url="/denied" authentication-failure-url="/accessdenied" />
    <logout logout-success-url="/logout" />
</http>

<authentication-manager alias="authenticationManager">
    <authentication-provider>
        <user-service>
            <user name="lokesh" password="password" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

</beans:beans>

this is spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**"  location="/resources/" />

<!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
<context:component-scan base-package="com.kartuku.somuploader, com.kartuku.somuploader.controller, com.kartuku.somuploader.service, com.kartuku.somuploader.model"/>

<!-- Resolve view name into jsp file located on /WEB-INF -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<!-- MySQL Datasource with Commons DBCP connection pooling -->
<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/prosom"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
</bean>

<!-- EntityManagerFactory -->
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- Transaction Manager -->
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<!-- another bean -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10000000"/>
</bean>

<!-- Enable @Transactional annotation -->
<tx:annotation-driven/>

</beans>

Try removing @ResponseBody from TemplateController. This may not a Spring Security related issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM